日期:2014-05-19  浏览次数:20906 次

求一段获取表格内容的正则表达式
<table   width= '500 '   bgcolor=#EEEEEE>
<tr> <td> 14:59:41 </td> <td> 商品A </td> <td> 9.310元 </td> <td> 100个 </td> </tr>
<tr> <td> 14:59:30 </td> <td> 商品B </td> <td> 9.320元 </td> <td> 50个 </td> </tr>
<tr> <td> 14:59:30 </td> <td> 商品C </td> <td> 9.310元 </td> <td> 20个 </td> </tr>
</table>

有上面一个表格我想获取他的行数据
14:59:41   商品A   9.310元   100个
14:59:30   商品B   9.320元   50个
14:59:30   商品C   9.310元   20个

以便插入数据库里
我该怎么写正则表示式
谢谢

------解决方案--------------------
http://www.cnblogs.com/time-is-life/articles/335554.html
------解决方案--------------------
依我之见最好把整个表格的HTML插入数据库。
------解决方案--------------------
<td> (? <time> \d+:\d+:\d+).* <td> (? <name> .+) <td> (? <price> \d+.\d+元).+ <td> (? <amount> \d+个)

match.Group[ "time "] match.Group[ "name "] match.Group[ "price "] match.Group[ "amount "]
------解决方案--------------------
如果格式固定,可以简单的这样写


MatchCollection mc = Regex.Matches(yourStr, @ " <td> (? <time> [^ <]*) </td> <td> (? <name> [^ <]*) </td> <td> (? <price> [^ <]*) </td> <td> (? <num> [^ <]*) </td> ", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
richTextBox2.Text += m.Groups[ "time "].Value + "\n ";
richTextBox2.Text += m.Groups[ "name "].Value + "\n ";
richTextBox2.Text += m.Groups[ "price "].Value + "\n ";
richTextBox2.Text += m.Groups[ "num "].Value + "\n ";
}

如果可能有多种格式,楼主需要说明一下,需要具体问题具体分析,单就一个实例写出来的未必通用
------解决方案--------------------
学习
------解决方案--------------------
用反向搜索那个,发回重写
------解决方案--------------------
晕,我foreah的时候少了{}...
foreach (Match m in matchs){
Response.Write(m.Groups[ "time "].Value + " <span style= 'width:10px; '> </span> ");
Response.Write(m.Groups[ "name "].Value + " <span style= 'width:10px; '> </span> ");
Response.Write(m.Groups[ "money "].Value + " <span style= 'width:10px; '> </span> ");
Response.Write(m.Groups[ "count "].Value + " <span style= 'width:10px; '> </span> ");
Response.Write( " <br> ");
}

发现我的反向搜索没有什么实际意义了。。。