PCRE、PCRE2 以及PCRE++ 使用教程

来源:互联网 发布:软件验收报告模板 编辑:程序博客网 时间:2024/05/21 16:17
1.准备工作 :请自行编译


PCRE、PCRE2 以及PCRE++ 库的源码,PCER2 参考我的博客。  

2.VS2008 建立一个win32 程序 ,工程名PcreDemo。

3.将所需的DLL 和 lib 拷贝进工程相应目录,并进行相关设置

4.  pcre 简单使用

 pcre  *re; 
 const char *error; 
 int  erroffset; 
 int  ovector[OVECCOUNT]; 
 int  rc;
 string strOrg = "I am hk!";
 string strRex = "\\b\\w+\\b";
 printf("前言:以下例子只是非常简单的应用,仅供参考!\n   pcreD.dll   pcre2.dll  pcre++.dll  pcreD.lib   pcre2.lib  pcre++.lib 请按照文档网址自行下载并编译!\n");
 printf("\n\n\n");
 printf(" PCRE 1 库   应用实例:\n\n");
 printf("字符串是 : %s\n", strOrg.c_str());
 printf("正则表达式是: %s\n\n\n", strRex.c_str());
 char *p = (char *) strOrg.c_str();
 re = pcre_compile(strRex.c_str(),       // pattern, 输入参数,将要被编译的字符串形式的正则表达式,
  PCRE_CASELESS|PCRE_GLOBAL,// options, 输入参数,用来指定编译时的一些选项
  &error,       // errptr, 输出参数,用来输出错误信息
  &erroffset,   // erroffset, 输出参数,pattern中出错位置的偏移量
  NULL);        // tableptr, 输入参数,用来指定字符表,一般情况用NULL
 // 返回值:被编译好的正则表达式的pcre内部表示结构
 if (re == NULL) {                 //如果编译失败,返回错误信息
  printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
  return false;
 }
 rc = pcre_exec(re, NULL, p, strlen(p), 0, 0, ovector, OVECCOUNT);
 if (rc < 0) {                     //如果没有匹配,返回错误信息
  if (rc == PCRE_ERROR_NOMATCH)
   printf("Sorry, no match ...\n");
  else
   printf("Matching error %d\n", rc);
  pcre_free(re);
  return false;
 }
 while ( ( rc = pcre_exec(re, NULL, p, strlen(p), 0, 0, ovector, OVECCOUNT)) != PCRE_ERROR_NOMATCH )
 {
  char *substring_start = p + ovector[0];
  int substring_length = ovector[1] - ovector[0];
  char matched[1024];
  memset( matched, 0, 1024 );
  strncpy( matched, substring_start, substring_length );
  //strDes = matched;
  printf( "全部的匹配结果有:%s\n", matched );

  p += ovector[1]; 
  if (*p == '\0')
  {
   break;
  }
 }
 pcre_free(re);                     // 编译正则表达式re 释放内存

5 .pcre2 简单使用

 printf(" PCRE 2 库  应用实例:\n\n");
 int error_code = 0;
 PCRE2_SIZE error_offset = 0;
 int nOptions =  0; 
// const char *pError; 
 string strOrg2 = "I am hk!";
 string strRex2 = "\\b\\w+\\b";

 printf("字符串是 : %s\n", strOrg2.c_str());
 printf("正则表达式是: %s\n\n\n", strRex2.c_str());
 //going to compile the regular expression pattern, and handle  any errors that are detected
  pcre2_code *recm = pcre2_compile(reinterpret_cast<PCRE2_SPTR>(strRex2.c_str()), PCRE2_ZERO_TERMINATED, 0 , &error_code, &error_offset, NULL);
  //Compilation failed: print the error message and exit
  if (re == NULL)
  {
   PCRE2_UCHAR buffer[256];
   pcre2_get_error_message(error_code, buffer, sizeof(buffer));
   printf("PCRE2 compilation failed at offset %d: %s\n", (int)error_offset,buffer);
   return 1;
  }
 // Using this function ensures that the block is exactly the right size for the number of capturing parentheses in the pattern.
  pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(recm, NULL);
  int rc2 = 0;
  int start_offset = 0;
  unsigned int match_index = 0;
  rc2= pcre2_match(recm, reinterpret_cast<PCRE2_SPTR>(strOrg2.c_str()), strOrg2.length(), start_offset, 0, match_data, NULL);
  //if(rc < 0)
  //{
  // 
  //}
  //else
  //{
  // int pos = start_offset;
 
  // while ((rc = pcre2_match(re, reinterpret_cast<PCRE2_SPTR>(strStruff.c_str()), strStruff.length(), start_offset, 0, match_data, NULL)) > 0)
  // {
  //  pos = start_offset;
  // }
  //}
  if (rc2 < 0)
    {
    switch(rc2)
   {
   case PCRE2_ERROR_NOMATCH: printf("No match\n"); break;
   /*
   Handle other special cases if you like
   */
   default: printf("Matching error %d\n", rc2); break;
   }
    pcre2_match_data_free(match_data);   /* Release memory used for the match */
    pcre2_code_free(recm);                 /* data and the compiled pattern. */
    return 1;
    }
  //going to compile the regular expression pattern, and handle  any errors that are detected
  while ((rc2 = pcre2_match(recm, reinterpret_cast<PCRE2_SPTR>(strOrg2.c_str()), strOrg2.length(), start_offset, 0, match_data, NULL)) > 0)
  {
    //Match succeded. Get a pointer to the output vector, where string offsets are stored.
    PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
    int i = 0;
    for (i = 0; i < rc2; i++)
    {
     char *substring_start = (char *)strOrg2.c_str() + ovector[2*i];
     int substring_length = ovector[2*i+1] - ovector[2*i];
     char matched[1024];
     memset( matched, 0, 1024 );
     strncpy( matched, substring_start, substring_length );
     printf( "全部匹配结果有  :%s\n",matched );
    }
   start_offset = ovector[2*(i-1) + 1];
   if ( strOrg2[start_offset] == '\0')
   {
    break;
   }
  }
  pcre2_match_data_free(match_data);
  pcre2_code_free(recm);

6.pcre++简单使用

  printf(" PCRE++库 应用实例:\n\n");
 /*
     * define a string with a regular expression
     */
  printf("官方例子如下:\n");
    string expression = "(\\b\\w+\\b)";

    /*
     * this is the string in which we want to search
     */
    string stuff = "I am hk!";

    cout << "  searching in \"" << stuff << "\" for regex \""
  << expression << "\":" << endl;

    /*
     * Create a new Pcre object, search case-insensitive ("i")
     */
 pcrepp::Pcre reg(expression, "i");
   
    /*
     * see if the expression matched
     */
    if(reg.search(stuff) == true) {

      /*
       * see if the expression generated any substrings
       */
      if(reg.matches() >= 1) {

 /*
  * print out the number of substrings
  */
 cout << "  generated " << reg.matches() << " substrings:" << endl;
  
 /*
  * iterate over the matched sub strings
  */
 for(int pos=0; pos < reg.matches(); pos++) {
   /* print out each substring */
   cout << "  substring " << pos << ": " << reg[pos];   // also possible: reg.get_match(pos);
   /* print out the start/end offset of the current substring
    * within the searched string(stuff)
    */
   cout << " (start: " << reg.get_match_start(pos) << ", end: "
        << reg.get_match_end(pos) << ")" << endl;
 }
      }
      else {
 /*
  * we had a match, but it generated no substrings, for whatever reason
  */
 cout << "   it matched, but there where no substrings." << endl;
      }
    }
    else {
      /*
       * no match at all
       */
      cout << "   didn't match." << endl;
    }

 printf("我自己做相关封装如下:\n");
 pcrepp::Pcre regg(expression);
 int npos = 0;
 if ( !regg.search(stuff, npos))
 {
  return false;
 }
 while(regg.search(stuff, npos))
 {
  if(regg.matches() >= 1)
  {
   string strMatched = regg.get_match(0);
   printf("全部匹配结果有:%s\n",strMatched.c_str());
   npos = regg.get_match_end(0) + 1;
  }
 }

7. OK!完成啦,有什么问题大家可以给我留言哦!