日期:2014-05-20  浏览次数:21077 次

在html中用正则表达式提取table块的问题,很很很急用,请助我解决。已经没分了。谢谢。
在html中用正则表达式提取table块的问题,很很很急用,请助我解决。已经没分了。谢谢。


在一篇html文档中,包括一个table表格(暂时叫母表),在这个table表格中,有两个子table表格。分别以这两个子table表格中的[abcdefg]和[1234567890]这两个单元格为标记,将这个table表格(母表)从这篇html中提取出来。

我用: <table[^> ]*> [^> ]* <tr> [\s\S].*?\[abcdefg\][\s\S]*?\[1234567890\][\s\S]*? </tr> [^> ]* </table> ,只能提取其中的一部分,而不能提取整个表格。

以下只列出了我需要提取的部分。

……省略了前面的html
<table   border= "1 "   width= "100% "   cellpadding= "2 "   style= "border-collapse:   collapse ">
<tr>
<td   bgcolor= "#ECECEC "   height= "24 ">
<table   border= "1 "   width= "100% "   cellpadding= "2 "   style= "border-collapse:   collapse ">
<tr>
<td> [abcdefg] </td>
<td> 测试单元格 </td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table   border= "1 "   width= "100% "   cellpadding= "0 "   style= "border-collapse:   collapse "   cellspacing= "0 ">
<tr>
<td> [1234567890] </td>
<td> [123456789x] </td>
</tr>
<tr>
<td> [123456789y] </td>
<td> [1234567890] </td>
</tr>
</table>
</td>
</tr>
<tr>
<td   bgcolor= "#FAFAFA "> 底部单元格 </td>
</tr>
</table>
……省略了后面的html

------解决方案--------------------
string regexStr = " <table.+?> [\\s\\S]+ <table.+?> [\\s\\S]+ </table> [\\s\\S]+ <table.+?> [\\s\\S]+ </table> [\\s\\S]+ </table> ";
PS:没有上下文,写出来的正则可能不对。
------解决方案--------------------
up