*******正则表达式(div标签内容提取)

OceanRain 2009-08-14 11:33:05
1.获取远程页面的内容:
........其它html略........
<DIV class="news_rbox mart10 border1" id=ShowPhoto>
<DIV class=news_rtit1>
<DIV class=news_rnav5><a>中国</a><img src='' /></DIV>
</DIV>
</DIV>
........其它html略...........



2.要求结果:
<DIV class=news_rtit1>
<DIV class=news_rnav5><a>中国</a><img src='' /></DIV>
</DIV>



3.问题:如果提取标签为DIV且id=ShowPhoto的内容?
...全文
1177 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2009-08-14
  • 打赏
  • 举报
回复
结果取m.Groups[2].Value
-过客- 2009-08-14
  • 打赏
  • 举报
回复
晕死,明显跟你给的例子不一致。。。

<div class="news_rbox mart10 border1" id="ShowPhoto">

算了,给你写通用了,不管效率了
Regex reg = new Regex(@"(?is)<div\b(?:(?!id=).)*id=(['""]?)ShowPhoto\1[^>]*>((?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!)))</div>");
OceanRain 2009-08-14
  • 打赏
  • 举报
回复
想取<div class="news_rbox mart10 border1" id="ShowPhoto"> </div>之间的内容,上面的正则取不出.
OceanRain 2009-08-14
  • 打赏
  • 举报
回复
源文件:
<div class="news_rbox mart10 border1" id="ShowPhoto">
<div class="news_rtit1">
<div class="news_rnav5"></div>
</div>



<div style="padding:5px" class="c333">



<div class="news_rboxx"><div style="height:100px; overflow:hidden"><a href="http://photos.jinti.com/Channel_15569" target="_blank"><img src=http://pic.jinti.com/member/459491/140_105_20098149456705.jpg border="0" /></div>
<a href="http://photos.jinti.com/Channel_15569" target="_blank">华丽的美人鱼</a></div>


<div class="news_rboxx"><div style="height:100px; overflow:hidden"><a href="http://photos.jinti.com/Channel_15568" target="_blank"><img src=http://pic.jinti.com/member/459491/140_105_200981485528705.jpg border="0" /></div>
<a href="http://photos.jinti.com/Channel_15568" target="_blank">碎花蓝的猜想</a></div>


<div class="news_rboxx"><div style="height:100px; overflow:hidden"><a href="http://photos.jinti.com/Channel_15567" target="_blank"><img src=http://pic.jinti.com/member/459491/140_105_20098138562705.jpg border="0" /></div>
<a href="http://photos.jinti.com/Channel_15567" target="_blank">黄奕柔美小乔造型曝光</a></div>


<div class="news_rboxx"><div style="height:100px; overflow:hidden"><a href="http://photos.jinti.com/Channel_15566" target="_blank"><img src=http://pic.jinti.com/member/459491/140_105_200981291112705.jpg border="0" /></div>
<a href="http://photos.jinti.com/Channel_15566" target="_blank">安室奈美惠代言变宫...</a></div>


</div>
</div>
-过客- 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 oceanrain 的回复:]

[/Quote]

什么意思,3楼代码不符合要求?
OceanRain 2009-08-14
  • 打赏
  • 举报
回复
-过客- 2009-08-14
  • 打赏
  • 举报
回复

Regex reg = new Regex(@"(?is)(?<=<div\b(?:(?!id=).)*id=ShowPhoto[^>]*>)(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))(?=</div>)");


参考

【分享】正则平衡组应用场景分析及性能优化
angieduan 2009-08-14
  • 打赏
  • 举报
回复

Regex expression = new Regex("<div(?:.|\\n)*?id=ShowPhoto(?:.|\\n)*?>((?:.|\\n)*?)</div>", RegexOptions.IgnoreCase);
Group g = expression.Matches(text)[1].Groups[1];
OceanRain 2009-08-14
  • 打赏
  • 举报
回复
顶下
-过客- 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 oceanrain 的回复:]
http://auto.jinti.com/iframe/WebRight.aspx这个是源页面,从这个页面提取,上面的正则把 <div class="news_rbox mart10 border1" id="ShowPhoto"> 标签外的div也取出来了.
[/Quote]

你的那个方法里data传的是什么?按你给的这个页面,里面根本就没有id="ShowPhoto"的div

还有,给一下你是如何使用那个正则的代码

Regex reg = new Regex(@"(?is)<div\b(?:(?!id=).)*id=(['""]?)ShowPhoto\1[^>]*>((?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!)))</div>");
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[2].Value + "\n";
}
qq904492758 2009-08-14
  • 打赏
  • 举报
回复
你用xml 转一下去节点多好啊 更容易控制啊
OceanRain 2009-08-14
  • 打赏
  • 举报
回复
通过下面代码获取http://auto.jinti.com/iframe/WebRight.aspx页面的源文件
public static string PostData(string url, string data)
{
string info = string.Empty;
try
{
CookieContainer cc = new CookieContainer();
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = cc;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.GetRequestStream();
byte[] byteArray = Encoding.UTF8.GetBytes(data);
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Uri responseUri = response.ResponseUri;
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
info = readStream.ReadToEnd();
}
catch{}

return info;
}
OceanRain 2009-08-14
  • 打赏
  • 举报
回复
http://auto.jinti.com/iframe/WebRight.aspx这个是源页面,从这个页面提取,上面的正则把<div class="news_rbox mart10 border1" id="ShowPhoto"> 标签外的div也取出来了.
纯纯璐璐 2009-08-14
  • 打赏
  • 举报
回复
上面说的很对,就正则匹配从match集合里取,

110,571

社区成员

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

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

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