C++基本没有正则表达式功能,当然像Boost里提供了正则。本文来源于博客园园友的一篇文章,请看:

C/C++ 字符串模糊匹配

很早之前就看过这篇文章,原作者的需求很明确、代码实现也很好。

之所以又写这篇文章,是因为原作者只介绍了在Linux系统下直接调用系统函数fnmatch即可实现,而没有考虑在Windows在的使用。

本人这周看了下Google-glog代码,恰巧发现了一个类似fnmatch的简单实现,因此综合起来提供了一个跨平台的接口。

直接拿原作者的需求为例(再次感谢原作者大熊先生,我这是拿来主义了):

需求:

  准入授权配置文件有时候分了好几个维度进行配置,例如 company|product|sys这种格式的配置:

1.配置 "sina|weibo|pusher" 表示 sina公司weibo产品pusher系统能够准入,而"sina|weibo|sign"不允许准入

2.配置 "sina|*|pusher” 表示sina公司所有产品的pusher系统都能够准入

3.配置 “*|*|pusher” 表示所有公司的所有产品的pusher系统都能够准入

代码实现如下:

 #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 = ;
size_t s = ;
while ()
{
if (p == patt_len && s == str_len)
return true;
if (p == patt_len)
return false;
if (s == str_len)
return p+ == patt_len && pattern[p] == '*';
if (pattern[p] == str[s] || pattern[p] == '?')
{
p += ;
s += ;
continue;
}
if (pattern[p] == '*')
{
if (p+ == patt_len) return true;
do
{
if (SafeFNMatch(pattern+(p+), patt_len-(p+), str+s, str_len-s))
{
return true;
}
s += ;
} while (s != str_len); return false;
} return false;
}
} /**注意:Windows平台下尚未实现最后一个参数flags的功能!!!*/
int fnmatch_win(const char *pattern, const char *name, int flags = )
{
if(SafeFNMatch(pattern,strlen(pattern),name,strlen(name)))
return ;
else
return FNM_NOMATCH;
} #else
#include <fnmatch.h>
#endif int main()
{
const char* orgin_str = "sina|weibo|pusher";
char pattern_arr[][] = {
{"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[]); vector<char *> vec_str;
for(int i = ; 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 = ; 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 ;
}

完整代码请看:字符串模糊匹配(fnmatch).cpp。输出如下:

当然,fnmatch的实现是源码可见的,也可以直接把Linux下的实现改改用在Windows平台。

fnmatch源码实现: http://www.opensource.apple.com/source/sudo/sudo-16/sudo/fnmatch.c

fnmatch使用参考: http://linux.die.net/man/3/fnmatch

C++实现的字符串模糊匹配的更多相关文章

  1. 简单易用的字符串模糊匹配库Fuzzywuzzy

    简单易用的字符串模糊匹配库Fuzzywuzzy 阅读目录 FuzzyWuzzy 简介 安装 用法 已知移植 FuzzyWuzzy 简介 FuzzyWuzzy 是一个简单易用的模糊字符串匹配工具包.它依 ...

  2. python 字符串模糊匹配 Fuzzywuzzy

    Python提供fuzzywuzzy模块,不仅可用于计算两个字符串之间的相似度,而且还提供排序接口能从大量候选集中找到最相似的句子. (1)安装 pip install fuzzywuzzy (2)接 ...

  3. mybatis字符串模糊匹配

    1.  参数中直接加入%%,注意不需要加两个单引号,加了就会出错,因为系统会自动为字符串类型加上两个单引号 <select id="selectPersons" result ...

  4. .NET ->> 分享一个字符串模糊匹配指数的方法

    链接: http://www.tsjensen.com/blog/post/2011/05/27/Four+Functions+For+Finding+Fuzzy+String+Matches+In+ ...

  5. HDU 2585 Hotel(字符串的模糊匹配+递归)

    Problem Description Last year summer Max traveled to California for his vacation. He had a great tim ...

  6. mysql 两张表字段模糊匹配--字符串拼接函数

    concat(A,B,C,...)  拼接字符串 例如concat('123','***','345')  =>123***345 SELECT concat( substr(t1.CODE, ...

  7. dev 中 字符串转中文拼音缩写,对grid列表进行模糊匹配,grid获取焦点行,gridlookupedit控件用拼音模糊匹配下拉选项

    番外篇:. //该方法是将字符串转化为中文拼音的首写字母大写, public static string RemoveSpecialCharacters(string str){try{if (str ...

  8. 使用vlookup的模糊匹配和字符串拼接

    1,=IF(ISNA(VLOOKUP("*"&$D2&"*",$A$2:$A$43,1,FALSE))=FALSE,TRUE,FALSE) 2, ...

  9. sql模糊匹配中%、_的处理

    防sql注入之模糊匹配中%._处理: StringBuilder sbSql = new StringBuilder(); sbSql.Append(@"SELECT * from tabl ...

随机推荐

  1. 20155236 2016-2017-2 《Java程序设计》第七周学习总结

    20155236 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 Calendar是个抽象类,java.util.GregorianCalendar是其子类, ...

  2. Windows could not set the offline local information.Error code:0X80000001解决方法

    我的笔记本是联想Y460(白色) 昨天在重装系统的时候遇到如下错误:Windows could not set the offline local information.Error code:0X8 ...

  3. Android绘图板的开发

    >>继承自View >>使用Canvas绘图 每次View组件上的图形状态数据发生了改变,都应该通知View组件重写调用onDraw(Canvas canvas)方法重绘该组件 ...

  4. 图片适应bitmap的大小 http上传文件

    image.setAdjustViewBounds(true); http上传文件 http://www.eoeandroid.com/thread-90209-1-1.html http://www ...

  5. Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏

    E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Android学习整理之Activity篇

    一.Activity概念介绍 activity属于android的四大组件之一(其他的三个: Content provider,Broadcast receiver,Service),它可以理解为一个 ...

  7. AtCoder - 4351 Median of Medians(二分+线段树求顺序对)

    D - Median of Medians Time limit : 2sec / Memory limit : 1024MB Score : 700 pointsProblem Statement ...

  8. Python学习-1.安装Python

    到Python的官方网站 https://www.python.org/downloads/ 下载官方的安装包 https://www.python.org/ftp/python/3.4.1/pyth ...

  9. [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]

      Updating a Dictionary  In this problem, a dictionary is collection of key-value pairs, where keys ...

  10. Android开源库集合(控件)

    RecycleView: RecycleView功能增强 https://github.com/Malinskiy/SuperRecyclerView RecycleView功能增强(拖拽,滑动删除, ...