Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1670236
  • 博文数量: 4
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7710
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-21 17:07
个人简介

linux and c

文章分类

全部博文(4)

文章存档

2015年(2)

2011年(1)

2008年(1)

我的朋友

分类: C/C++

2015-11-15 12:13:39

Linux C 正则表达式, 已经循环匹配所有符合的字符串

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <regex.h>

  4. char *string = " i love you love baby ";
  5. //截取子字符串
  6. char* substring(const char* str, size_t begin, size_t len)
  7. {
  8.    if (str == 0 || strlen(str) == 0 || strlen(str) < begin || strlen(str) < (begin+len))
  9.       return 0;

  10.    return strndup(str + begin, len);   //使用strndup()出来的字符串,需要free()
  11. }
  12. //正则匹配, 只搜索字符串中第一个符合的子字符串
  13. int regex_search(const char *str, const char *regex, regmatch_t pmatch[], int nmatch)
  14. {
  15.    regex_t preg;

  16.    bzero(&preg, sizeof(regex_t));

  17.    if(regcomp(&preg, regex, REG_EXTENDED) == 0)
  18.    {
  19.       if(regexec(&preg, str, nmatch, pmatch, 0) == 0)
  20.          return 0;
  21.       else
  22.          return -1;
  23.    }
  24.    else
  25.    {
  26.       return -2;
  27.    }
  28. }
  29. //统计符合正则的子字符串的所有个数
  30. int substr_count(char *str, const char *sub)
  31. {
  32.    int i=0;
  33.    char *p = str;
  34.    while(p)
  35.    {
  36.       if((p=strstr(p,sub)))
  37.       {
  38.          i++;
  39.          p+=strlen(sub);
  40.       }
  41.       else
  42.       {
  43.          break;
  44.       }
  45.    }
  46.    return i;
  47. }

  48. int main()
  49. {
  50.    //正则表达式
  51.    char *regex = "lo(v)(e)";
  52.    int i=0,len=0;
  53.    //统计左括号的个数,用于判断正则表达式中有多少(),+1是因为除()外,整个正则字符串也算一个, 虽然不准,但也可以
  54.    int nmatch = substr_count(regex, "(")+1;
  55.    //根据regex中的()个数,设置一次正则匹配会有多少个分组
  56.    regmatch_t pmatch[nmatch];
  57.    int k=0;
  58.    int ind=0;
  59.    int j = 0;
  60.    //循环进行正则匹配
  61.    for( j=0; j<strlen(string); )
  62.    {
  63.       char *p;

  64.       p=string+j;
  65.       //单次正则匹配
  66.       if(regex_search(p, regex, pmatch, nmatch) == 0)
  67.       {
  68.          //遍历单次匹配的所有分组
  69.          for(i=0; i<nmatch;i++)
  70.          {
  71.             //单分组的模式匹配开始位置
  72.             if(pmatch[i].rm_so == -1 )
  73.                continue;

  74.             //第一个分组是整个完整正则表达式 l(o)(v)e匹配,比如  "i love you love you" ,记录love中e的位置,赋给p,用于对后面的字符串继续匹配正则
  75.             if(i==0)
  76.                j=j+pmatch[i].rm_eo;

  77.             //获取匹配串的长度
  78.             len = (pmatch[i].rm_eo - pmatch[i].rm_so);

  79.             //输出字符串
  80.             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) ));
  81.          }
  82.       }
  83.       else
  84.       {
  85.          break;
  86.       }
  87.    }
  88.    return 0;
  89. }
  90. //输出
  91. userdeMac:stardict user$ ./a.out
  92. found 3-7 love
  93. found 5-6 v
  94. found 6-7 e

  95. found 5-9 love
  96. found 7-8 v
  97. found 8-9 e

 

阅读(505) | 评论(1) | 转发(0) |
0

上一篇:垃圾的评论管理

下一篇:没有了

给主人留下些什么吧!~~

cnscn20082015-11-15 12:14:17

/**
  preg   存放编译后的结果
  regex  正则表达式字符串
  cflags 
         REG_EXTENDED 使用扩展正则表达式
         REG_ICASE    忽略大小写
         REG_NOSUB    忽略参数nmatch和pmatch
         REG_NEWLINE  特殊字符不以换行字符做比较


  int regcomp(regex_t *preg, const ch