当前位置: 代码迷 >> ASP.NET >> 正则表达示有关问题!如果匹配出"上一页“
  详细解决方案

正则表达示有关问题!如果匹配出"上一页“

热度:2455   发布时间:2013-02-25 00:00:00.0
正则表达示问题!如果匹配出"下一页“?
如下html
<div class="pages" id="pageDivTop"><a href="http://www.tianya.cn/publicforum/content/stocks/1/727805.shtml">首页</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1046188.shtml">802</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1046773.shtml">803</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1047058.shtml">804</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1047264.shtml">805</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1047606.shtml">806</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1048293.shtml">807</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1048651.shtml">808</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1049081.shtml">809</a><em class="current">810</em><a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">811</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">下一页</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">末页</a><a href="#adsp_content_replybox_frame_1">回复此贴</a><span>共811页</span><form

如何只匹配出
<a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">下一页</a>?
列在我只匹配出所所有的超链接来循环判断。很不好
其实我是须要这个下一页的链接

------解决方案--------------------------------------------------------
(?i)<a\b[^>]*?href=(["']?)([^"']*?)\1[^>]*?>下一页</a>

C# code
string str=@"HTML源码";str=str.Match(str,@"(?i)<a\b[^>]*?href=([""']?)([^""']*?)\1[^>]*?>下一页</a>").Groups[2].Value;
------解决方案--------------------------------------------------------
var s = '<div class="pages" id="pageDivTop"><a href="http://www.tianya.cn/publicforum/content/stocks/1/727805.shtml">首页</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1046188.shtml">802</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1046773.shtml">803</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1047058.shtml">804</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1047264.shtml">805</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1047606.shtml">806</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1048293.shtml">807</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1048651.shtml">808</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1049081.shtml">809</a><em class="current">810</em><a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">811</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">下一页</a><a href="http://www.tianya.cn/publicforum/content/stocks/1/1050069.shtml">末页</a><a href="#adsp_content_replybox_frame_1">回复此贴</a><span>共811页</span><form';

var reg = new RegExp("<a[^>]*>[^>]*下一页[^>]*<\s?\/\s?a>", "g");
var txt = "";
s = reg.exec(s).pop();
alert(s);
  相关解决方案