没有合适的资源?快使用搜索试试~ 我知道了~
首页C/C++实现字符串模糊匹配
需求: 准入授权配置文件有时候分了好几个维度进行配置,例如 company|product|sys这种格式的配置: 1.配置 “sina|weibo|pusher” 表示 sina公司weibo产品pusher系统能够准入,而”sina|weibo|sign”不允许准入 2.配置 “sina|*|pusher” 表示sina公司所有产品的pusher系统都能够准入 3.配置 “*|*|pusher” 表示所有公司的所有产品的pusher系统都能够准入 … 类似还有很多场景,好了,简单的东西不扯蛋了. 实现: 面对这个需求我第一时间想的是如何设计模式串,如何快速实现功能,因为我现
资源详情
资源评论
资源推荐

C/C++实现字符串模糊匹配实现字符串模糊匹配
需求:
准入授权配置文件有时候分了好几个维度进行配置,例如 company|product|sys这种格式的配置:
1.配置 “sina|weibo|pusher” 表示 sina公司weibo产品pusher系统能够准入,而”sina|weibo|sign”不允许准入
2.配置 “sina|*|pusher” 表示sina公司所有产品的pusher系统都能够准入
3.配置 “*|*|pusher” 表示所有公司的所有产品的pusher系统都能够准入
…
类似还有很多场景,好了,简单的东西不扯蛋了.
实现:
面对这个需求我第一时间想的是如何设计模式串,如何快速实现功能,因为我现在写的是一个C服务,所以我首先出现在
我脑海的是一大堆strchr(XXX, ‘*’), strchr(XXX, ‘|’)等等东西,后面发现这个东西没有必要自己造轮子,有现成的函数可以用,
那就是fnmatch.
google了一下,发现fnmatch的资料并不是很多,大部分还都是讲php函数的,所以没办法,只能自己写写测测了.
#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;
}
}
结果:
实验一把,结果还不赖,完全满足需求:



















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0