c语言正则表达式匹配URL问题
发布时间:2020-12-14 01:53:21 所属栏目:百科 来源:网络整理
导读:自己用socket写了一个小程序去获取网站的源代码后,用pcre去获取网站的URL ,有以下疑问: 部分源码: [code=c]pcre *re; const char *error; int erroffset; int ovector[5000]; int rc,i; char pattern [] = "http://([w-]+.)+[w-]+(/[w- ./?%%=]*)
自己用socket写了一个小程序去获取网站的源代码后,用pcre去获取网站的URL
,有以下疑问: 部分源码: [code=c]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,&error,&erroffset,NULL))) { printf("%d:%sn",erroffset,error); return 1; } rc = pcre_exec(re,NULL,sendbuf,strlen(sendbuf),ovector,5000); if (rc < 0) { //如果没有匹配,返回错误信息 if (rc == PCRE_ERROR_NOMATCH) printf("Sorry,no match ...n"); else printf("Matching error %dn",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: %.*sn",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 。。。。 问什么会有这么大的差别啊。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |