您的位置:程序门 -> .net技术 -> c#



急急急急:url匹配错误!!!!!!!!(在线等)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


急急急急:url匹配错误!!!!!!!!(在线等)
发表于:2007-01-05 22:18:38 楼主
有一个这样的html文档:
<a   href= " ">   </a>
<a   href= "http://classad.163.com/html/area/110/index.html "> 陕 </a>
<a   href= "http://classad.163.com/html/area/341/index.html "> 宁 </a>  
<a   href= "http://classad.163.com/html/area/577/index.html "> 藏 </a>  
<a   href= "http://classad.163.com/html/area/325/index.html "> 宜昌 </a>
<a   href= "http://classad.163.com/html/area/31/index.html "> 郑州 </a>  
<a   href= " ">   </a>
<a   href= "http://classad.163.com/html/area/254/index.html "> 南通 </a>  
<a   href= " ">   </a>  
<a   href= "http://classad.163.com/html/area/313/index.html "> 荆门 </a>  
<a   href= " ">   </a>  
<a   href= "http://classad.163.com/html/area/81/index.html "> 日照 </a>
<a   href= "http://classad.163.com/html/area/36/index.html "> 洛阳 </a>  
<a   href= "http://classad.163.com/html/area/393/index.html ">   朝阳区 </a>  
<a   href= " ">   </a>  
用正则表达式:
<a\s+href\s*=\s*[ " ¦ ']?(? <uri> [^ ' ">       ]*)[ " ¦ ']?[^ <> ]*> \s*( <[^ <> ]+> )*(? <title> [^ <> ]*)( <[^ <> ]+> )*\s* </a>
是可以完全匹配的,也就是说可以匹配15结果(我在测试工具上运行过);但是我写的代码只可以匹配11个,我的代码是:
string   htmlregexepression=@ " <a\s+href\s*=\s*[ " " ¦ ']?(? <uri> [^ ' " ">   ]*)[ " " ¦ ']?[^ <> ]*> \s*( <[^ <> ]+> )*(? <title> [^ <> ]*)( <[^ <> ]+> )*\s* </a> ";
regex   linksexpression   =new   regex(   htmlregexepression,
regexoptions.multiline   ¦   regexoptions.ignorecase   );
matchcollection   matchs   =   linksexpression.matches(pagehtmlcontent);

请问是不是htmlregexepression转换错误了??请高手急救!!!!!
无限感谢!!!!!!!!!!!!!!!!!!!!!
发表于:2007-01-06 00:19:251楼 得分:0
我试了,可以匹配出15个来啊,这是我用的测试代码
matchcollection   mc=regex.matches(yourstr,@ " <a\s+href\s*=\s*[ " " ¦ ']?(? <uri> [^ ' " ">   ]*)[ " " ¦ ']?[^ <> ]*> \s*( <[^ <> ]+> )*(? <title> [^ <> ]*)( <[^ <> ]+> )*\s* </a> ");
foreach(match   m   in   mc)
{
        richtextbox1.text   +=m.groups[0].value+ "\n ";
}

要睡觉了,没太仔细看,不过应该不用像你写的那么麻烦的,明天再看下吧
发表于:2007-01-06 11:05:092楼 得分:0
又看了下,你给的代码应该没问题,要看一下你取出matchs   后的处理代码了,如果你是对uri
和title进行处理,这两项在 <a   href= " ">   </a> 内取到的都是零长度字符串,但这样取到的结果应该是10个,而不是11个才对,还是给一下你的处理代码吧


快速检索

最新资讯
热门点击