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

flytang 2007-01-05 10: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转换错误了??请高手急救!!!!!
无限感谢!!!!!!!!!!!!!!!!!!!!!
...全文
126 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2007-01-06
  • 打赏
  • 举报
回复
我试了,可以匹配出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
  • 打赏
  • 举报
回复
又看了下,你给的代码应该没问题,要看一下你取出Matchs 后的处理代码了,如果你是对uri
和title进行处理,这两项在<a href=""> </a>内取到的都是零长度字符串,但这样取到的结果应该是10个,而不是11个才对,还是给一下你的处理代码吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧