求一正则,高手帮帮忙

Error_Code 2009-10-12 12:23:22

<table border="0" width="556" cellpadding="0" bgcolor="#FFFFFF">
<tr><td width="130" valign="top"><p align="center"><img border="0" src="face/m02.gif"><br><Br>第<font color="#FF0000">1068</font>条留言</p></td>
<td width="425" valign="top">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody><tr><td width="43" colspan="2" rowspan="2" height="29">
<img border="0" src="/img/1_r2_c2.gif" width="43" height="29"></td>
<td height="10" background="/img/1_r2_c41.gif"></td>
<td width="37" colspan="2" rowspan="2" height="29">
<img border="0" src="/img/1_r2_c6.gif" width="37" height="29"></td></tr><tr><td height="19"><table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td width="60%"><div style="width:120;color:0099FF;filter:glow(color=00FF66, strength=1)">天之痕</div></td><td width="40%" align="right"></td></tr></table></td></tr><tr><td width="10" background="/img/1_r4_c2.gif"></td><td width="27"></td><td width="100%" height="50" style="table-layout:fixed; WORD-WRAP:break-word; word-break:break-all">

来看看
</td><td width="22"></td><td width="15" background="/img/1_r4_c2.gif"></td></tr><tr><td width="43" colspan="2" rowspan="2" height="26"><img border="0" src="/img/1_r6_c2.gif" width="43" height="27"></td><td height="16" align="right">

<img border="0" src="/img/ip.gif" width="16" height="15" title="116.19.*.*"><font color=#336600>[2009-6-25 16:41:41]</font></td><td width="43" colspan="2" rowspan="2" height="26"><img border="0" src="/img/1_r6_c6.gif" width="37" height="27"></td></tr><tr><td height="9" background="/img/1_r2_c4.gif"></td></tr></tbody></table>

</td></tr>
<tr><td width="640" colspan="2">
<hr SIZE="1" color=#F0F0F0></td></tr></table>


留言本搬家 希望把以前的数据保存
这个是别人的程序 拿不到数据库 所以只有用正则提出来

一个页面有10个上面这样的table
希望提出下面红色的数据
1068 条数
天之痕 发帖人
来看看 内容 (可能包含 空格 回车 和UBB code)
2009-6-25 16:41:41 时间
...全文
134 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Error_Code 2009-10-12
  • 打赏
  • 举报
回复
重新发一下 跑到里面了

<table border="0" width="556" cellpadding="0" bgcolor="#FFFFFF">
<tr><td width="130" valign="top"><p align="center"><img border="0" src="face/m02.gif">
<br><Br>第<font color="#FF0000">1068</font>条留言</p></td>
<td width="425" valign="top">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody><tr><td width="43" colspan="2" rowspan="2" height="29">
<img border="0" src="/img/1_r2_c2.gif" width="43" height="29"></td>
<td height="10" background="/img/1_r2_c41.gif"></td>
<td width="37" colspan="2" rowspan="2" height="29">
<img border="0" src="/img/1_r2_c6.gif" width="37" height="29"></td></tr><tr><td height="19">
<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td width="60%">
<div style="width:120;color:0099FF;filter:glow(color=00FF66, strength=1)">天之痕奔波</div></td>
<td width="40%" align="right"></td></tr></table>
</td></tr><tr><td width="10" background="/img/1_r4_c2.gif">
</td><td width="27"></td>
<td width="100%" height="50" style="table-layout:fixed; WORD-WRAP:break-word; word-break:break-all">

来看看 还是一片枯叶飘过。。。休~~~~呜~呼呼~
</td><td width="22"></td><td width="15" background="/img/1_r4_c2.gif"></td></tr><tr>
<td width="43" colspan="2" rowspan="2" height="26">
<img border="0" src="/img/1_r6_c2.gif" width="43" height="27">
</td><td height="16" align="right">

<img border="0" src="/img/ip.gif" width="16" height="15" title="116.19.*.*">
<font color=#336600>[2009-6-25 16:41:41]</font></td>
<td width="43" colspan="2" rowspan="2" height="26">
<img border="0" src="/img/1_r6_c6.gif" width="37" height="27">
</td></tr><tr><td height="9" background="/img/1_r2_c4.gif">
</td></tr></tbody></table>

</td></tr>
<tr><td width="640" colspan="2">
<hr SIZE="1" color=#F0F0F0></td></tr></table>
Error_Code 2009-10-12
  • 打赏
  • 举报
回复
就是匹配出来的结果是"\r\r" 实际上在\r后面还有数据

Ok了 我在匹配之前把所有的'\r' '\n' 都remove了 结贴
jiangshun 2009-10-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 error_code 的回复:]
3Q
不过2楼的 有点点问题 就是很多地方的

m.Groups["actor"] 和 m.Groups["content"]取到的是"\r\r"
用记事本打开查看 就显示的是一个黑方块 内容的第一个字符
[/Quote]

什么意思?
Error_Code 2009-10-12
  • 打赏
  • 举报
回复
3Q
不过2楼的 有点点问题 就是很多地方的

m.Groups["actor"] 和 m.Groups["content"]取到的是"\r\r"
用记事本打开查看 就显示的是一个黑方块 内容的第一个字符
路人乙e 2009-10-12
  • 打赏
  • 举报
回复
string html = "。。。";//整个HTML页面内容,包含多条留言
Regex reg_tb = new Regex(@"<table border=""0"" width=""556"" cellpadding=""0"" bgcolor=""#FFFFFF"">.+?<hr SIZE=""1"" color=#F0F0F0></td></tr></table>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match mat = reg_tb.Match(html);
while(mat.Success)
{
string table = mat.Value;
//使用2楼的代码即可
mat = reg_tb.Match(html, mat.Index+mat.Length);
}
dryzha 2009-10-12
  • 打赏
  • 举报
回复
string s = @"<table border=""""0"""" width=""556"" cellpadding=""0"" bgcolor=""#FFFFFF"">
<tr><td width=""130"" valign=""top""><p align=""center""><img border=""0"" src=""face/m02.gif"">
<br><Br>第<font color=""#FF0000"">1068</font>条留言</p></td>
<td width=""425"" valign=""top"">
<table border=""0"" width=""100%"" cellspacing=""0"" cellpadding=""0"">
<tbody><tr><td width=""43"" colspan=""2"" rowspan=""2"" height=""29"">
<img border=""0"" src=""/img/1_r2_c2.gif"" width=""43"" height=""29""></td>
<td height=""10"" background=""/img/1_r2_c41.gif""></td>
<td width=""37"" colspan=""2"" rowspan=""2"" height=""29"">
<img border=""0"" src=""/img/1_r2_c6.gif"" width=""37"" height=""29""></td></tr><tr><td height=""19"">
<table border=""0"" width=""100%"" cellspacing=""0"" cellpadding=""0""><tr><td width=""60%"">
<div style=""width:120;color:0099FF;filter:glow(color=00FF66, strength=1)"">天之痕奔波</div></td>
<td width=""40%"" align=""right""></td></tr></table>
</td></tr><tr><td width=""10"" background=""/img/1_r4_c2.gif"">
</td><td width=""27""></td>
<td width=""100%"" height=""50"" style=""table-layout:fixed; WORD-WRAP:break-word; word-break:break-all"">

来看看 还是一片枯叶飘过。。。休~~~~呜~呼呼~
</td><td width=""22""></td><td width=""15"" background=""/img/1_r4_c2.gif""></td></tr><tr>
<td width=""43"" colspan=""2"" rowspan=""2"" height=""26"">
<img border=""0"" src=""/img/1_r6_c2.gif"" width=""43"" height=""27"">
</td><td height=""16"" align=""right"">

<img border=""0"" src=""/img/ip.gif"" width=""16"" height=""15"" title=""116.19.*.*"">
<font color=#336600>[2009-6-25 16:41:41]</font></td>
<td width=""43"" colspan=""2"" rowspan=""2"" height=""26"">
<img border=""0"" src=""/img/1_r6_c6.gif"" width=""37"" height=""27"">
</td></tr><tr><td height=""9"" background=""/img/1_r2_c4.gif"">
</td></tr></tbody></table>

</td></tr>
<tr><td width=""640"" colspan=""2"">
<hr SIZE=""1"" color=#F0F0F0></td></tr></table>";
s.Replace(@"<[^<>]+>","")
jiangshun 2009-10-12
  • 打赏
  • 举报
回复

string s = @"<table border=""""0"""" width=""556"" cellpadding=""0"" bgcolor=""#FFFFFF"">
<tr><td width=""130"" valign=""top""><p align=""center""><img border=""0"" src=""face/m02.gif"">
<br><Br>第<font color=""#FF0000"">1068</font>条留言</p></td>
<td width=""425"" valign=""top"">
<table border=""0"" width=""100%"" cellspacing=""0"" cellpadding=""0"">
<tbody><tr><td width=""43"" colspan=""2"" rowspan=""2"" height=""29"">
<img border=""0"" src=""/img/1_r2_c2.gif"" width=""43"" height=""29""></td>
<td height=""10"" background=""/img/1_r2_c41.gif""></td>
<td width=""37"" colspan=""2"" rowspan=""2"" height=""29"">
<img border=""0"" src=""/img/1_r2_c6.gif"" width=""37"" height=""29""></td></tr><tr><td height=""19"">
<table border=""0"" width=""100%"" cellspacing=""0"" cellpadding=""0""><tr><td width=""60%"">
<div style=""width:120;color:0099FF;filter:glow(color=00FF66, strength=1)"">天之痕奔波</div></td>
<td width=""40%"" align=""right""></td></tr></table>
</td></tr><tr><td width=""10"" background=""/img/1_r4_c2.gif"">
</td><td width=""27""></td>
<td width=""100%"" height=""50"" style=""table-layout:fixed; WORD-WRAP:break-word; word-break:break-all"">

来看看 还是一片枯叶飘过。。。休~~~~呜~呼呼~
</td><td width=""22""></td><td width=""15"" background=""/img/1_r4_c2.gif""></td></tr><tr>
<td width=""43"" colspan=""2"" rowspan=""2"" height=""26"">
<img border=""0"" src=""/img/1_r6_c2.gif"" width=""43"" height=""27"">
</td><td height=""16"" align=""right"">

<img border=""0"" src=""/img/ip.gif"" width=""16"" height=""15"" title=""116.19.*.*"">
<font color=#336600>[2009-6-25 16:41:41]</font></td>
<td width=""43"" colspan=""2"" rowspan=""2"" height=""26"">
<img border=""0"" src=""/img/1_r6_c6.gif"" width=""37"" height=""27"">
</td></tr><tr><td height=""9"" background=""/img/1_r2_c4.gif"">
</td></tr></tbody></table>

</td></tr>
<tr><td width=""640"" colspan=""2"">
<hr SIZE=""1"" color=#F0F0F0></td></tr></table>";


Regex reg = new Regex(@"第.*>(?<count>\d+)<.*条留言[\s\S]*
<div.*>(?<actor>.*)<\/div>[\s\S]*
<td\swidth\=""100%"".*>(?<content>[^<]+)<[\s\S]*
\[(?<date>.*)\][\s\S]*");
MatchCollection mc = reg.Matches(s);
foreach (Match m in mc)
{
Console.WriteLine("条数:" + m.Groups["count"].ToString() + "\r\n"
+ "发帖人:" + m.Groups["actor"].ToString() + "\r\n"
+ "内容:" + m.Groups["content"].ToString() + "\r\n"
+ "时间:" + m.Groups["date"].ToString());
}

/* *****************************结果*********************
条数:1068
发帖人:天之痕奔波
内容:

来看看 还是一片枯叶飘过。。。休~~~~呜~呼呼~

时间:2009-6-25 16:41:41

*/

110,536

社区成员

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

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

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