C/C++實(shí)現(xiàn)字符串模糊匹配
需求:
準(zhǔn)入授權(quán)配置文件有時(shí)候分了好幾個(gè)維度進(jìn)行配置,例如 company|product|sys這種格式的配置:
1.配置 "sina|weibo|pusher" 表示 sina公司weibo產(chǎn)品pusher系統(tǒng)能夠準(zhǔn)入,而"sina|weibo|sign"不允許準(zhǔn)入
2.配置 "sina|*|pusher” 表示sina公司所有產(chǎn)品的pusher系統(tǒng)都能夠準(zhǔn)入
3.配置 “*|*|pusher” 表示所有公司的所有產(chǎn)品的pusher系統(tǒng)都能夠準(zhǔn)入
…
類似還有很多場(chǎng)景,好了,簡(jiǎn)單的東西不扯蛋了.
實(shí)現(xiàn):
面對(duì)這個(gè)需求我第一時(shí)間想的是如何設(shè)計(jì)模式串,如何快速實(shí)現(xiàn)功能,因?yàn)槲椰F(xiàn)在寫(xiě)的是一個(gè)C服務(wù),所以我首先出現(xiàn)在我腦海的是一大堆strchr(XXX, ‘*'), strchr(XXX, ‘|')等等東西,后面發(fā)現(xiàn)這個(gè)東西沒(méi)有必要自己造輪子,有現(xiàn)成的函數(shù)可以用,那就是fnmatch.
google了一下,發(fā)現(xiàn)fnmatch的資料并不是很多,大部分還都是講php函數(shù)的,所以沒(méi)辦法,只能自己寫(xiě)寫(xiě)測(cè)測(cè)了.
#include <iostream>
#include <fnmatch.h>
#include <vector>
using namespace std;
int main()
{
const char* orgin_str = "sina|weibo|pusher";
char pattern_arr[][20] = {
{"sina|*|pusher"},
{"sina|*|*"},
{"*|weibo|*"},
//不能被匹配的
{"sina|pic|*"},
{"*|*|sign"},
{"*|weibo|sign"},
{"*|pic|sign"},
{"sina|pic|sign"},
{"*|*|*"}
};
static int pattern_arr_size = sizeof(pattern_arr) / sizeof(pattern_arr[0]);
vector<char *> vec_str;
for(int i = 0; i < pattern_arr_size; i ++)
{
vec_str.push_back(pattern_arr[i]);
}
int ret;
int z = 0;
while(z < 1){
for(int i = 0; i < vec_str.size(); i++)
{
ret = fnmatch(vec_str.at(i), orgin_str, FNM_PATHNAME);
if(FNM_NOMATCH == ret){
cout<<"sorry I'm failed ["<< vec_str.at(i) <<"]"<<endl;
}
}
++z;
}
}
結(jié)果:
實(shí)驗(yàn)一把,結(jié)果還不賴,完全滿足需求:

需求滿足了,我擔(dān)心的還有一個(gè)問(wèn)題,那就是性能,注釋掉cout輸出,將while z語(yǔ)句調(diào)至1,000,000,重新編譯跑一下:
time ./fnmatch

看來(lái)效率還不錯(cuò),2.1s 進(jìn)行了100W次匹配,平均2us一次,性能要求也滿足了...
附:上面文章只介紹了在Linux系統(tǒng)下直接調(diào)用系統(tǒng)函數(shù)fnmatch即可實(shí)現(xiàn),而沒(méi)有考慮在Windows在的使用。
本人這周看了下Google-glog代碼,恰巧發(fā)現(xiàn)了一個(gè)類似fnmatch的簡(jiǎn)單實(shí)現(xiàn),因此綜合起來(lái)提供了一個(gè)跨平臺(tái)的接口。
#ifdef OS_WINDOWS
/* Bits set in the FLAGS argument to `fnmatch'. copy from fnmatch.h(linux) */
#define FNM_PATHNAME (1 << 0) /* No wildcard can ever match `/'. */
#define FNM_NOESCAPE (1 << 1) /* Backslashes don't quote special chars. */
#define FNM_PERIOD (1 << 2) /* Leading `.' is matched only explicitly. */
#define FNM_NOMATCH 1
#define fnmatch fnmatch_win
/**copy from Google-glog*/
bool SafeFNMatch(const char* pattern,size_t patt_len,const char* str,size_t str_len)
{
size_t p = 0;
size_t s = 0;
while (1)
{
if (p == patt_len && s == str_len)
return true;
if (p == patt_len)
return false;
if (s == str_len)
return p+1 == patt_len && pattern[p] == '*';
if (pattern[p] == str[s] || pattern[p] == '?')
{
p += 1;
s += 1;
continue;
}
if (pattern[p] == '*')
{
if (p+1 == patt_len) return true;
do
{
if (SafeFNMatch(pattern+(p+1), patt_len-(p+1), str+s, str_len-s))
{
return true;
}
s += 1;
} while (s != str_len);
return false;
}
return false;
}
}
/**注意:Windows平臺(tái)下尚未實(shí)現(xiàn)最后一個(gè)參數(shù)flags的功能?。?!*/
int fnmatch_win(const char *pattern, const char *name, int flags = 0)
{
if(SafeFNMatch(pattern,strlen(pattern),name,strlen(name)))
return 0;
else
return FNM_NOMATCH;
}
#else
#include <fnmatch.h>
#endif
int main()
{
const char* orgin_str = "sina|weibo|pusher";
char pattern_arr[][20] = {
{"sina|*|pusher"},
{"sina|*|*"},
{"*|weibo|*"},
//不能被匹配的
{"sina|pic|*"},
{"*|*|sign"},
{"*|weibo|sign"},
{"*|pic|sign"},
{"sina|pic|sign"},
{"*|*|*"}
};
static int pattern_arr_size = sizeof(pattern_arr) / sizeof(pattern_arr[0]);
vector<char *> vec_str;
for(int i = 0; i < pattern_arr_size; i ++)
{
vec_str.push_back(pattern_arr[i]);
}
std::cout << "Origin Str: " << orgin_str << "\n\n";
int ret;
for(int i = 0; i < vec_str.size(); i++)
{
ret = fnmatch(vec_str.at(i), orgin_str, FNM_PATHNAME);
if(ret == FNM_NOMATCH)
{
cout<<"sorry, I'm failed: ["<< vec_str.at(i) <<"]\n";
}
else
{
cout<<"OK, I'm success: ["<< vec_str.at(i) <<"]\n";
}
}
return 0;
}
輸出如下:

- opencv3/C++ FLANN特征匹配方式
- C++深入學(xué)習(xí)之徹底理清重載函數(shù)匹配
- C++ string 字符串查找匹配實(shí)例代碼
- C++中用棧來(lái)判斷括號(hào)字符串匹配問(wèn)題的實(shí)現(xiàn)方法
- C++實(shí)現(xiàn)將長(zhǎng)整型數(shù)轉(zhuǎn)換為字符串的示例代碼
- C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)的問(wèn)題詳解
- C++ 將字符串值賦給CHAR數(shù)組的實(shí)現(xiàn)
- C++ 字符串string和整數(shù)int的互相轉(zhuǎn)化操作
- C++獲取字符串長(zhǎng)度的幾個(gè)函數(shù)方式
- c++字符串分割的方法
- c++ 實(shí)現(xiàn)文件逐行讀取與字符匹配
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)大整數(shù)加減運(yùn)算詳解
大數(shù)運(yùn)算,顧名思義,就是很大的數(shù)值的數(shù)進(jìn)行一系列的運(yùn)算。本文通過(guò)實(shí)例演示如何進(jìn)行C語(yǔ)言中的大整數(shù)加減運(yùn)算,有需要的可以參考借鑒。2016-08-08
C++數(shù)據(jù)結(jié)構(gòu)之鏈表詳解
這篇文章主要介紹了C++數(shù)據(jù)結(jié)構(gòu)之鏈表的創(chuàng)建的相關(guān)資料,希望通過(guò)本文幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2021-08-08
C++之std::vector刪除元素的幾種方式及區(qū)別說(shuō)明
這篇文章主要介紹了C++之std::vector刪除元素的幾種方式及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
C語(yǔ)言實(shí)現(xiàn)跨文件傳輸數(shù)據(jù)的幾種方式
C語(yǔ)言是一種強(qiáng)大的、通用的編程語(yǔ)言,常用于系統(tǒng)級(jí)編程,包括硬件交互,如中斷處理和數(shù)據(jù)采集,在本文中,我們將深入探討如何使用C語(yǔ)言進(jìn)行跨文件數(shù)據(jù)傳輸,文中有相關(guān)的代碼供大家參考,需要的朋友可以參考下2024-08-08
C++中一維數(shù)組與指針的關(guān)系詳細(xì)總結(jié)
以下是對(duì)C++中一維數(shù)組與指針的關(guān)系進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09
C語(yǔ)言實(shí)現(xiàn)繪制貝塞爾曲線的函數(shù)
貝塞爾曲線,又稱貝茲曲線或貝濟(jì)埃曲線,是應(yīng)用于二維圖形應(yīng)用程序的數(shù)學(xué)曲線。本文將利用C語(yǔ)言實(shí)現(xiàn)繪制貝塞爾曲線的函數(shù),需要的可以參考一下2022-12-12

