首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

pcrecpp高级运用

2013-03-17 
pcrecpp高级使用昨天简单介绍了一下pcrecpp的使用,常用的匹配函数包括FullMatch和PartilaMatch等,FullMatc

pcrecpp高级使用

昨天简单介绍了一下pcrecpp的使用,常用的匹配函数包括FullMatch和PartilaMatch等,FullMatch和PartilaMatch对于捕获参数的个数都有限制,最多能传16个捕获参数。而且不能够根据模式中的捕获情况动态设定捕获参数。查看了一个pcrecpp的文档,其中提及DoMatch函数能够做更普适的匹配操作。但是关于DoMatch函数的介绍也仅限于此,google搜索也没找到更多的材料。

因为要用,只好去看了一下pcrecpp的源码,最后终于搞定了,写一个能够根据传入的模式和匹配串执行动态匹配的程序。编码过程中对DoMatch函数有了一些深入的了解,发现DoMatch函数主要是调用DoMatchImpl函数来实现的,并且实际上FullMatch和PartilaMatch等匹配函数也大多是调用DoMatchImpl来实现的,这是一个私有函数,无法直接使用。

下面还是介绍一下DoMatch函数的使用吧。函数的原型为:

bool precpp::RE:DoMatch( const StringPiece &  text,Anchor  anchor,int *  consumed,const Arg *const *  args,int  n);

 各个参数的含义如下:

1. const StringPiece &  text : StringPiece 是pcrecpp中定义的类型,暂时理解为一个字符串就行了

2.Anchor  anchor:是锚点的意思,UNANCHORED,ANCHOR_START,ANCHOR_BOTH。具体含义自己理解吧,pcrecpp的实现中FullMatch设置的选项是ANCHOR_BOTH,PartilaMatch设置的是AUNANCHORED。

3.int *  consumed:匹配消耗目标串中的字符数

4.const Arg *const *  args:这个参数的类型可以作为考察对const定义理解的考题,呵呵。我的理解是一个args是一个指针,这个指向一个常量,该常量又是一个指向常量Arg类型的指针。其实函数需要的就是一个数组,数组中的元素是指向常量Arg类型的常量指针。

5.int  n:这是需要捕获的子串的个数,与args数组的大小相等。

好了,下面介绍一下我的小程序吧。

 #include<iostream> #include<string> #include<string.h> #include<pcrecpp.h> #include<vector> using namespace std;  //计算string中的匹配括号数,如果括号未完全匹配,则返回-1,否则返回括号对数 int countParenthesis(string pt); //返回0表示匹配成功 其它表示失败 //匹配的结果以string的形式依次存储在matched中 int  match(string pattern,string subject,bool isFullMatch,vector<string> &matched) {     pcrecpp::RE_Options opt;     opt.set_caseless(true);      try{         pcrecpp::RE re = pcrecpp::RE(pattern,opt);          int num = countParenthesis(pattern);//根据模式中的括号数目来决定捕获字符串的个数         if(num>0){             string *ss = new string[num];             const pcrecpp::Arg **args = new const pcrecpp::Arg*[num];  //定义args,注意它的类型哦              for(int i=0;i<num;i++){                 args[i] = new pcrecpp::Arg(&ss[i]); //需要使用pcrecpp::Arg的构造函数             }              int consumed = 0;             if(isFullMatch){                 re.DoMatch(subject,pcrecpp::RE::ANCHOR_BOTH,&consumed,args,num);             }else{                 re.DoMatch(subject,pcrecpp::RE::UNANCHORED,&consumed,args,num);             }              int ret = -1;             if( re.NumberOfCapturingGroups() > 0){                 matched.clear();                 for(int i=0;i<num;i++){                     matched.push_back(ss[i]);                 }                 ret = 0;//匹配成功,返回值设为0             }              for(int i=0;i<num;i++){                 delete args[i] ;             }             delete[] ss;             delete[] args;              return ret;          }else if(num==0){             if(isFullMatch){                 bool res = re.FullMatch(subject);                 return res==true ? 0 : -1;             }else{                 bool res  = re.PartialMatch(subject);                 return res==true ? 0 : -1;             }         }else{             cout<<"the Parenthesis is not paired!"<<endl;             return -1;         }     }catch(exception ex){         cout<<"some exception happened! please check you pattern";         return -1;     } }  int main() {     string content("<<<中国><E><B>><C>A>");     pcrecpp::RE_Options  options;     options.set_caseless(true);//不区分大小写     string pattern = ("(<(<<中国><E><B>>)?<(<F><G>)?C>A>)");    //可以更改传入的目标串和模式动态决定捕获多少个子串,都以string存起来     vector<string> matched;     match(pattern,content,true,matched);     cout<<matched.size()<<endl;     for(vector<string>::iterator itr = matched.begin(); itr!=matched.end(); itr++)         cout<<*itr<<endl     return 0;

还是C比较简洁,看着舒服,C++以后要优化下

弄了半天这个东西,希望对大家有帮助。

热点排行