正则表达式字符串取反问题

bian8021 2011-08-24 02:36:11
我写正则表达式解析html数据,比如下面的数据:
<tr>....................</tr>
<tr>....................</tr>
<tr>....................</tr>
<tr class="blueBorder3">

<td width=37>12</td>
<td width=47>2078</td>
<td align="left"><span class="f14Blue"><a href="http://club.it.sohu.com/r-zz0072-287374-0-12-900.html" target="_blank">各种奇异耐旱植物:龙血树分泌深红色树脂</a></span><span class="IMGspace"><img src="http://js4.club.sohu.com/newbbs/images/b09b.gif" alt="已被置顶" /><img src="http://js4.club.sohu.com/newbbs/images/b10b.gif" alt="已加精华" /></span></td>
<td class="nameID" width=94 ><a href="http://club.sohu.com/read_user.php?userCN=king_fung@sohu" target="_blank">王者之凤</a></td>
<td width=94><span class="date">11-08-21 11:05</span></td>
<td class="nameID" style="padding-left:7px" width=101><a href="http://club.sohu.com/read_user.php?userCN=chengyna123%40sohu" target="_blank">天使就在人间</a></td>

</tr>

我想写正则表达式把r-zz0072-287374-0-12-900.html(红色部分)的这段<tr></tr>给找出来,我写了下面的表达式:
Regex reList = new Regex(string.Format(@"(?is)<tr[^>]*?>.*?{0}.*?</tr>", fileName));

但是这样取tr从第一个就开始取了,我想在中间.*?这个,改为除去</tr>这个字符串,我不会写,哪位帮忙一下。
...全文
309 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenquan123 2011-08-26
  • 打赏
  • 举报
回复

(?:exp)匹配exp但不捕获匹配的文本,也不给此分组分配组号
(?:(?!exp).)* 匹配后面不是exp的任意其他数
bian8021 2011-08-26
  • 打赏
  • 举报
回复
to huangwenquan123:

谢谢啦,我试了,成功了.能否解释一下:?:(?!</?tr).)*?,有点看不懂.
sunny906 2011-08-24
  • 打赏
  • 举报
回复
哦,是这个

Match match = Regex.Match(s, @"<tr[^>]*?>(?:[\s\S]*?/r-zz0072-287374-0-12-900.html[\s\S]*?)</tr>");
Console.WriteLine(match.Value);
sunny906 2011-08-24
  • 打赏
  • 举报
回复

Match match = Regex.Match(s, "<td[^>]*?><span[^>]*?><a href=\"http://[^/]*?/([^\"]*?)\"[^>]*?>");
Console.WriteLine(match.Groups[1].Value);
huangwenquan123 2011-08-24
  • 打赏
  • 举报
回复
            string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex(@"(?is)<tr[^>]*?>(?:(?!</?tr).)*?r-zz0072-287374-0-12-900.html.*?</tr>");
Console.WriteLine(reg.Match(str).Value);

110,532

社区成员

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

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

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