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. 48 Fixing relationship Problems with Humor 用幽默解决人际关系问题

    48 Fixing relationship Problems with Humor 用幽默解决人际关系问题 ①We've all heard that laughter is the best me ...

  2. Linux设置开机启动项

    第一种方式:ln -s 建立启动软连接 在Linux中有7种运行级别(可在/etc/inittab文件设置),每种运行级别分别对应着/etc/rc.d/rc[0~6].d这7个目录 Tips:/etc ...

  3. #ing#我的日常知识管理160421

    1. 知识来源:浏览是无时无刻的,知识爆炸的时代,信息在互联网上不再难以获得,却变得难以选择,取得有效的信息成了如今获取知识的重要环节…… 前端关注—— alloyteam.github.io  腾讯 ...

  4. C#-派生类

    VS派生类 继承.派生类 class AClass : FClass 构建函数继承 public classname (string astring): base(astring){};默认不继承基类 ...

  5. Tensorflow從源碼編譯

    从源代码构建 从源代码构建 TensorFlow pip 软件包并将其安装在 Ubuntu Linux 和 macOS 上.尽管这些说明可能适用于其他系统,但仅针对 Ubuntu 和 macOS 进行 ...

  6. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

  7. error: libXpm.(a|so)

    centos 6.5 安装php时老是报错,找了很久答案都是千篇一律且不起作用,最后找到一个答案,特记录在此 脚本: tar zxvf php-5.3.28.tar.gz && cd ...

  8. 评论:一套Developer Express控件包 For Delphi7

    http://www.2ccc.com/idea.asp?articleid=1675 (也可以查看盒子上这个帖子的内容) Developer Express Inc 系列控件组 for Delphi ...

  9. C99 中 main 函数的写法

    今天在论坛看见有人讨论 C 语言中 main 函数的写法,看到结论才知道 main 函数的正确写法. 被老谭酸菜坑了这么多年,还是记录下吧,或许以后某天不搞 .net,回去折腾 C 语言了. 写法1: ...

  10. SQL Server 索引基本概念与优化

    数据页和区 页 SQL Server 中的数据以“页”(Page)的形式保存数据,页是SQL Server 的IO单位,读/写一次至少是一页.一页为8K(8192byte). 页由三部分组成,页头,数 ...