自己用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:%s\n",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 %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 。。。。 问什么会有这么大的差别啊。
总结
以上是编程之家为你收集整理的c语言正则表达式匹配URL问题全部内容,希望文章能够帮你解决c语言正则表达式匹配URL问题所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
小编个人微信号 jb51ccc
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!