免费注册 查看新帖 |

Chinaunix

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

关于C/C++ 正则表达式库pcre的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-15 14:46 |只看该作者 |倒序浏览
我在联芯的手机平台上移植了个pcre库,

可发现不能匹配多个,

比如:

          int         erroffset = 0;
        int         ovector[30] = {0};
        int         iRet = 0;
        const pcre  *re;
        const char  *error;

        re = pcre_compile("1", 0, &error, &erroffset, NULL);

        iRet = pcre_exec(re, NULL, "11111", strlen("11111"), 0, 0, ovector, 30);

        pcre_free((void *)re);

        printf("The Matching Number is %d", iRet);


返回的iRet始终为1,应该为5才对,求解。。。

后来怀疑自己移植的库有问题,我又从网上下载了老外编译好的WIN32上的DLL,随便写了个类似的测试程序,结果依然如此。。

论坛徽章:
0
2 [报告]
发表于 2009-09-15 17:29 |只看该作者
顶上去,查找了和那多地方,实在想不通,不可能只匹配一个的

论坛徽章:
0
3 [报告]
发表于 2009-09-15 18:41 |只看该作者
我记得pcre_exec是匹配到符合条件的第一个就停止了,
你要的功能应该自己循环实现(每次匹配到指针往后挪)

pcre_exec的返回值也不是你说的意思,It returns offsets to captured substrings
举例来说
        char            src    [] = "111 <title>Hello World</title> 222";
        char            pattern   [] = "<title>(.*)</title>";

pcre_exec()的返回值会是2, 第1个是指整个pattern的匹配,第2个是指子表达式的匹配
(.*)里面的内容, 也就是“Hello World”

论坛徽章:
0
4 [报告]
发表于 2009-09-16 10:22 |只看该作者
原帖由 caijimin 于 2009-9-15 18:41 发表
我记得pcre_exec是匹配到符合条件的第一个就停止了,
你要的功能应该自己循环实现(每次匹配到指针往后挪)

pcre_exec的返回值也不是你说的意思,It returns offsets to captured substrings
举例来说
   ...


我目前就是用循环的方法用的。

但是我man了很多次,看了源码的函数头说明,感觉pcre应该有方法一次得到N次匹配的


/* This function applies a compiled re to a subject string and picks out
portions of the string if it matches. Two elements in the vector are set for
each substring: the offsets to the start and end of the substring.

Arguments:
  argument_re     points to the compiled expression
  extra_data      points to extra data or is NULL
  subject         points to the subject string
  length          length of subject string (may contain binary zeros)
  start_offset    where to start in the subject string
  options         option bits
  offsets         points to a vector of ints to be filled in with offsets
  offsetcount     the number of elements in the vector

Returns:          > 0 => success; value is the number of elements filled in
                  = 0 => success, but offsets is not big enough
                   -1 => failed to match
                 < -1 => some kind of unexpected problem
*/

PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,
  PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,
  int offsetcount)


这里说明的returns和你说的还是有很大出入的

论坛徽章:
0
5 [报告]
发表于 2009-09-16 10:38 |只看该作者
http://www.pcre.org/pcre.txt
重点看   How pcre_exec() returns captured substrings这一段

论坛徽章:
0
6 [报告]
发表于 2009-09-16 12:11 |只看该作者
原帖由 caijimin 于 2009-9-16 10:38 发表
http://www.pcre.org/pcre.txt
重点看   How pcre_exec() returns captured substrings这一段



好的,我再仔细看看,我英文烂,可能推敲要花点时间,呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP