昨天简单介绍了一下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数组的大小相等。

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

  1. #define PCRE_STATIC // 静态库编译选项
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <pcre.h>
  5. #define OVECCOUNT 30 /* should be a multiple of 3 */
  6. #define EBUFLEN 128
  7. #define BUFLEN 1024
  8. int main()
  9. {
  10. pcre *re;
  11. const char *error;
  12. int erroffset;
  13. int ovector[OVECCOUNT];
  14. int rc, i;
  15. char src [] = "111 <title>Hello World</title> 222"; // 要被用来匹配的字符串
  16. char pattern [] = "<title>(.*)</(tit)le>"; // 将要被编译的字符串形式的正则表达式
  17. printf("String : %s/n", src);
  18. printf("Pattern: /"%s/"/n", pattern);
  19. re = pcre_compile(pattern, // pattern, 输入参数,将要被编译的字符串形式的正则表达式
  20. 0, // options, 输入参数,用来指定编译时的一些选项
  21. &error, // errptr, 输出参数,用来输出错误信息
  22. &erroffset, // erroffset, 输出参数,pattern中出错位置的偏移量
  23. NULL); // tableptr, 输入参数,用来指定字符表,一般情况用NULL
  24. // 返回值:被编译好的正则表达式的pcre内部表示结构
  25. if (re == NULL) { //如果编译失败,返回错误信息
  26. printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
  27. return 1;
  28. }
  29. rc = pcre_exec(re, // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
  30. NULL, // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
  31. src, // subject, 输入参数,要被用来匹配的字符串
  32. strlen(src), // length, 输入参数, 要被用来匹配的字符串的指针
  33. 0, // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
  34. 0, // options, 输入参数, 用来指定匹配过程中的一些选项
  35. ovector, // ovector, 输出参数,用来返回匹配位置偏移量的数组
  36. OVECCOUNT); // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
  37. // 返回值:匹配成功返回非负数,没有匹配返回负数
  38. if (rc < 0) { //如果没有匹配,返回错误信息
  39. if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match .../n");
  40. else printf("Matching error %d/n", rc);
  41. pcre_free(re);
  42. return 1;
  43. }
  44. printf("/nOK, has matched .../n/n"); //没有出错,已经匹配
  45. for (i = 0; i < rc; i++) { //分别取出捕获分组 $0整个正则公式 $1第一个()
  46. char *substring_start = src + ovector[2*i];
  47. int substring_length = ovector[2*i+1] - ovector[2*i];
  48. printf("$%2d: %.*s/n", i, substring_length, substring_start);
  49. }
  50. pcre_free(re); // 编译正则表达式re 释放内存
  51. return 0;
  52. }
  1.  

C++的

  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<pcrecpp.h>
  5. #include<vector>
  6. using namespace std;
  7. //计算string中的匹配括号数,如果括号未完全匹配,则返回-1,否则返回括号对数
  8. int countParenthesis(string pt);
  9. //返回0表示匹配成功 其它表示失败
  10. //匹配的结果以string的形式依次存储在matched中
  11. int match(string pattern,string subject,bool isFullMatch,vector<string> &matched)
  12. {
  13. pcrecpp::RE_Options opt;
  14. opt.set_caseless(true);
  15. try{
  16. pcrecpp::RE re = pcrecpp::RE(pattern,opt);
  17. int num = countParenthesis(pattern);//根据模式中的括号数目来决定捕获字符串的个数
  18. if(num>0){
  19. string *ss = new string[num];
  20. const pcrecpp::Arg **args = new const pcrecpp::Arg*[num]; //定义args,注意它的类型哦
  21. for(int i=0;i<num;i++){
  22. args[i] = new pcrecpp::Arg(&ss[i]); //需要使用pcrecpp::Arg的构造函数
  23. }
  24. int consumed = 0;
  25. if(isFullMatch){
  26. re.DoMatch(subject,pcrecpp::RE::ANCHOR_BOTH,&consumed,args,num);
  27. }else{
  28. re.DoMatch(subject,pcrecpp::RE::UNANCHORED,&consumed,args,num);
  29. }
  30. int ret = -1;
  31. if( re.NumberOfCapturingGroups() > 0){
  32. matched.clear();
  33. for(int i=0;i<num;i++){
  34. matched.push_back(ss[i]);
  35. }
  36. ret = 0;//匹配成功,返回值设为0
  37. }
  38. for(int i=0;i<num;i++){
  39. delete args[i] ;
  40. }
  41. delete[] ss;
  42. delete[] args;
  43. return ret;
  44. }else if(num==0){
  45. if(isFullMatch){
  46. bool res = re.FullMatch(subject);
  47. return res==true ? 0 : -1;
  48. }else{
  49. bool res = re.PartialMatch(subject);
  50. return res==true ? 0 : -1;
  51. }
  52. }else{
  53. cout<<"the Parenthesis is not paired!"<<endl;
  54. return -1;
  55. }
  56. }catch(exception ex){
  57. cout<<"some exception happened! please check you pattern";
  58. return -1;
  59. }
  60. }
  61. int main()
  62. {
  63. string content("<<<中国><E><B>><C>A>");
  64. pcrecpp::RE_Options options;
  65. options.set_caseless(true);//不区分大小写
  66. string pattern = ("(<(<<中国><E><B>>)?<(<F><G>)?C>A>)");
  67. //可以更改传入的目标串和模式动态决定捕获多少个子串,都以string存起来
  68. vector<string> matched;
  69. match(pattern,content,true,matched);
  70. cout<<matched.size()<<endl;
  71. for(vector<string>::iterator itr = matched.begin(); itr!=matched.end(); itr++)
  72. cout<<*itr<<endl
  73. return 0;

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

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

版权声明:本文为mtcnn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/mtcnn/archive/2013/03/15/9410112.html