您的位置:首页 > 编程语言 > C语言/C++

c语言正则表达式匹配URL问题

2014-03-20 14:01 471 查看
自己用socket写了一个小程序去获取网站的源代码后,用pcre去获取网站的URL

,有以下疑问:

部分源码:

pcre *re;

const char *error;

int erroffset;

int  ovector[5000];

int  rc, i;

char  pattern [] = "http://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%%&=]*)?";

if(NULL == (re = pcre_compile(pattern,0,&error,&erroffset,NULL)))

{

printf("%d:%s\n", erroffset,error);

return 1;

}

rc = pcre_exec(re,NULL,sendbuf,strlen(sendbuf),0,0,ovector,5000);

if (rc < 0) {                     //如果没有匹配,返回错误信息

if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n");

else printf("Matching error %d\n", rc);

pcre_free(re);

return 1;

}

for (i = 0; i < rc; i++) {             //分别取出捕获分组 $0整个正则公式
$1第一个()

char *substring_start = sendbuf + ovector[2*i];

int substring_length = ovector[2*i+1] - ovector[2*i];

printf("$%2d: %.*s\n", i, substring_length, substring_start);

}

pcre_free(re);

[/code]执行结果为:

$ 0: http://s1.bdstatic.com/r/www/cache/static/global/img/icons_4879d3b8.png
$ 1: bdstatic.

$ 2: /r/www/cache/static/global/img/icons_4879d3b8.png

但是我用网上的正则表达式测试工具一样的正则表达式一样的文本出现的结果如下:
http://s1.bdstatic.com/r/www/cache/static/global/img/icons_4879d3b8.png http://s1.bdstatic.com/r/www/cache/static/global/img/icons_5a07887b.gif http://s1.bdstatic.com/r/www/cache/static/global/img/icons_4879d3b8.png http://s1.bdstatic.com/r/www/cache/static/global/img/icons_5a07887b.gif http://www.baidu.com/img/sug_bd.png http://s1.bdstatic.com/r/www/cache/static/global/img/wsCloseBtn2_4a84c812.png http://www.baidu.com/ http://www.baidu.com/s? http://www.baidu.com/s? http://www.baidu.com/gaoji/preferences.html
。。。。

问什么会有这么大的差别啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: