正则表达式获取html代码里的数据

gdk123 2012-04-19 05:09:31
<div class="blue14fontbold" id="new_page"><span><img height="11" width="6" src="http://www.m1905.com/m_images/images/pageleft.jpg">  </span><span>总数:

<b>23</b></span>  <a class="pre" href="http://www.m1905.com/yx/film/c1p0.html">上一页</a><u><b>1</b></u> <a href="http://www.m1905.com/yx/film/c1p2.html">2</a>

<a href="http://www.m1905.com/yx/film/c1p3.html">3</a> <a class="next" href="http://www.m1905.com/yx/film/c1p2.html">下一页</a><span>  <img height="11" width="6"

src="http://www.m1905.com/m_images/images/pageright.jpg"></span>
</div>


如上所示:用正则表达式匹配 <a href="http://www.m1905.com/yx/film/c1p2.html">2</a>

<a href="http://www.m1905.com/yx/film/c1p3.html">3</a>这部分内容里a标签href上的内容 即:http://www.m1905.com/yx/film/c1p2.html


...全文
134 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalmeeme 2012-04-19
  • 打赏
  • 举报
回复
foreach(Match match in Regex.Matches(@"(?is)<a\s+href=""(.+?)"">"))
Response.Write(match.Groups[1].Value+"<br/>");
EnForGrass 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
<div class="blue14fontbold" id="new_page"><span><img height="11" width="6" src="http://www.m1905.com/m_images/images/pageleft.jpg">&nbsp;&nbsp;</span><span>总数:

<b>23</b></span>&nbsp;&am……
[/Quote]

Dictionary<string, string> dic = new Dictionary<string, string>();
MatchCollection m = Regex.Matches(input, @"(?i)<a[^href]*\s*href=""([^>]+)"">(.*?)</a>");
foreach (Match mx in m)
{
dic.Add(mx.Groups[1].Value, mx.Groups[2].Value);
Console.WriteLine(mx.Groups[1].Value + "\t" + mx.Groups[2].Value);
}
/*
http://www.m1905.com/yx/film/c1p2.html 2
http://www.m1905.com/yx/film/c1p3.html 3
*/

q107770540 2012-04-19
  • 打赏
  • 举报
回复
(?i)(?<=<a\shref=").*?(?=")

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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