c语言验证url的正则表达式,c语言正则表达式匹配URL问题

自己用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,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 。。。。 问什么会有这么大的差别啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值