免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2783 | 回复: 3
打印 上一主题 下一主题

[C] Linux -- C -- regex 正则表达式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-11-16 10:20 |只看该作者 |倒序浏览
  1. 1.#include <stdio.h>

  2. 1.#include <strings.h>

  3. 2.#include <regex.h>

  4. 3.

  5. 4.char *string = " i love you love baby ";

  6. 5.
  7. 6.//截取子字符串

  8. 7.char* substring(const char* str, size_t begin, size_t len)

  9. 8.{

  10. 9.   if (str == 0 || strlen(str) == 0 || strlen(str) < begin || strlen(str) < (begin+len))

  11. 10.      return 0;

  12. 11.

  13. 12.   return strndup(str + begin, len);   //使用strndup()出来的字符串,需要free()

  14. 13.}

  15. 14.
  16. 15.//正则匹配, 只搜索字符串中第一个符合的子字符串

  17. 16.int regex_search(const char *str, const char *regex, regmatch_t pmatch[], int nmatch)

  18. 17.{

  19. 18.   regex_t preg;

  20. 19.

  21. 20.   bzero(&preg, sizeof(regex_t));

  22. 21.

  23. 22.   if(regcomp(&preg, regex, REG_EXTENDED) == 0)

  24. 23.   {

  25. 24.      if(regexec(&preg, str, nmatch, pmatch, 0) == 0)

  26. 25.         return 0;

  27. 26.      else

  28. 27.         return -1;

  29. 28.   }

  30. 29.   else

  31. 30.   {

  32. 31.      return -2;

  33. 32.   }

  34. 33.}

  35. 34.
  36. 35.//统计符合正则的子字符串的所有个数

  37. 36.int substr_count(char *str, const char *sub)

  38. 37.{

  39. 38.   int i=0;

  40. 39.   char *p = str;

  41. 40.   while(p)

  42. 41.   {

  43. 42.      if((p=strstr(p,sub)))

  44. 43.      {

  45. 44.         i++;

  46. 45.         p+=strlen(sub);

  47. 46.      }

  48. 47.      else

  49. 48.      {

  50. 49.         break;

  51. 50.      }

  52. 51.   }

  53. 52.   return i;

  54. 53.}

  55. 54.
  56. 55.

  57. 56.int main()

  58. 57.{
  59. 58.   //正则表达式

  60. 59.   char *regex = "lo(v)(e)";

  61. 60.   int i=0,len=0;

  62. 61.
  63. 62.   //统计左括号的个数,用于判断正则表达式中有多少(),+1是因为除()外,整个正则字符串也算一个, 虽然不准,但也可以
  64. 63.   int nmatch = substr_count(regex, "(")+1;

  65. 64.
  66. 65.   //根据regex中的()个数,设置一次正则匹配会有多少个分组

  67. 66.   regmatch_t pmatch[nmatch];

  68. 67.
  69. 68.   int k=0;

  70. 69.   int ind=0;

  71. 70.   int j = 0;
  72. 71.   //循环进行正则匹配

  73. 72.   for( j=0; j<strlen(string); )

  74. 73.   {

  75. 74.      char *p;

  76. 75.

  77. 76.      p=string+j;
  78. 77.      //单次正则匹配

  79. 78.      if(regex_search(p, regex, pmatch, nmatch) == 0)

  80. 79.      {
  81. 80.         //遍历单次匹配的所有分组

  82. 81.         for(i=0; i<nmatch;i++)

  83. 82.         {
  84. 83.            //单分组的模式匹配开始位置

  85. 84.            if(pmatch[i].rm_so == -1 )

  86. 85.               continue;
  87. 86.

  88. 87.            //第一个分组是整个完整正则表达式 l(o)(v)e匹配,比如  "i love you love you" ,记录love中e的位置,赋给p,用于对后面的字符串继续匹配正则

  89. 88.            if(i==0)

  90. 89.               j=j+pmatch[i].rm_eo;
  91. 90.

  92. 91.            //获取匹配串的长度

  93. 92.            len = (pmatch[i].rm_eo - pmatch[i].rm_so);
  94. 93.

  95. 94.            //输出字符串

  96. 95.            printf("found %lld-%lld %s \n", pmatch[i].rm_so, pmatch[i].rm_eo,substring(p,(int)pmatch[i].rm_so, (int)(pmatch[i].rm_eo - pmatch[i].rm_so) ));

  97. 96.         }

  98. 97.      }

  99. 98.      else

  100. 99.      {

  101. 100.         break;

  102. 101.      }

  103. 102.   }

  104. 103.   return 0;

  105. 104.}

  106. 105.
  107. 106.//输出

  108. 107.userdeMac:stardict user$ ./a.out

  109. 108.found 3-7 love

  110. 109.found 5-6 v

  111. 110.found 6-7 e
  112. 111.

  113. 112.found 5-9 love

  114. 113.found 7-8 v

  115. 114.found 8-9 e
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-11-16 10:49 |只看该作者
哪里的库?

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
3 [报告]
发表于 2015-11-16 13:03 |只看该作者
帖子啥意思?

学习分享吗?

论坛徽章:
3
综合交流区版块每日发帖之星
日期:2016-02-15 06:20:00操作系统版块每日发帖之星
日期:2016-02-15 06:20:00每日论坛发贴之星
日期:2016-02-15 06:20:00
4 [报告]
发表于 2015-12-23 10:28 来自手机 |只看该作者
没明白
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP