正则表达式问题 html取值

大Y 2012-03-14 06:37:35

<div class="fans_top" id="weibo_head">
<a href="http://baidu.com/" target="_blank">评论(0)</a>
<div>dddd</div>
</div>

<div class="ddd" id="ccc">
<a href="http://baidu.com/" target="_blank">评论(20)</a>
<a href="http://baidu.com/" target="_blank">评论(150)</a>
</div>



想得到的结果为:class为fans_top 和id为weibo_head里面包含的html值;
正则的写法为:(?<=<div[^>]+class="fans_top"[^>]+id="weibo_head"[^>]*>)[\s\S]*?(?=</div>)

但是在此DIv中不包含子div时,值是取的正确的,现在就是里面如果在出现一个div的话,结束的地方就产生了错误!


...全文
147 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
IssacChow 2012-03-15
  • 打赏
  • 举报
回复

恭喜楼主!!!
大Y 2012-03-15
  • 打赏
  • 举报
回复
下面还有一个解决方法!

(?<=<div[^>]+class="fans_top"[^>]+id="weibo_head"[^>]*>)(?:[\s\S]*?<div[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>)*[\s\S]*?(?=</div>)

是来自正则群里的标哥,呵呵
说一下正则群号为:47341579
大Y 2012-03-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 return_false 的回复:]

C# code
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string pattern = @"(?i)(?<=<div[^>]*?class=['""]?fa……
[/Quote]

测试,成功了,谢谢

  • 打赏
  • 举报
回复
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));
string pattern = @"(?i)(?<=<div[^>]*?class=['""]?fans_top['""]?[^>]*?id=['""]?weibo_head['""]?[^>]*?>)(?><div[^>]*>(?<Open>)|</div>(?<-Open>)|(?:(?!</?div\b)[\s\S])*)*(?=</div>)";
string temp_result = Regex.Match(tempStr,pattern).Value;
//<a href=\"http://baidu.com/\" target=\"_blank\">评论(0)</a>\r\n<div>dddd</div>
大Y 2012-03-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 porschev 的回复:]
C# code


string htmsStr = @"<div class=""fans_top"" id=""weibo_head"">
<a href=""http://baidu.com/"" target=""_blank"">评论(0)</a>
<div>dddd</div>
</div>

<div class=""ddd"" id=""ccc""……
[/Quote]

不对哇,不知道你测试没有呀!
取的结果为:
<a href="http://baidu.com/" target="_blank">评论(0)</a>
<div>dddd
后面还少一个</div>了,里面的那个div还是取的不对,应该是一个整体的
大Y 2012-03-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 porschev 的回复:]
意思是要这段?

<a href="http://baidu.com/" target="_blank">评论(0)</a>
<div>dddd</div>
[/Quote]

对的
porschev 2012-03-14
  • 打赏
  • 举报
回复



string htmsStr = @"<div class=""fans_top"" id=""weibo_head"">
<a href=""http://baidu.com/"" target=""_blank"">评论(0)</a>
<div>dddd</div>
</div>

<div class=""ddd"" id=""ccc"">
<a href=""http://baidu.com/"" target=""_blank"">评论(20)</a>
<a href=""http://baidu.com/"" target=""_blank"">评论(150)</a>
</div>
";
Regex re = new Regex("(?is)(?<=<div\\s*class=\"fans_top\"\\s*id=\"weibo_head\">).*?(?=</div>)", RegexOptions.None);
Match mt = re.Match(htmsStr);
Console.WriteLine(mt.Value);
Console.ReadLine();
porschev 2012-03-14
  • 打赏
  • 举报
回复

意思是要这段?

<a href="http://baidu.com/" target="_blank">评论(0)</a>
<div>dddd</div>

62,268

社区成员

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

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

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

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