日期:2014-05-18  浏览次数:20453 次

求正则表达式:html下的Table
字符串结构为
...
<form   id= "form "   >
<javascript> ... </javascript>
<table   id= "Top "   >
        <table> ... </table>
        <table> .. </table>
        <table> ... </table>
</table>
<javascript> ... </javascript>
<javascript> ... </javascript>
</form>
也就是说,页面中form   下面就是一个保护所有信息的Table,这个table下面有不确定的子table
现在我想取得这个最大table的,
不知道大家有什么好的解决方案呢?

------解决方案--------------------
<table[^> ]*> [^ <> ]*(((? 'Open ' <table[^> ]*> )[^ <> ]*)+((? '-Open ' </table> )[^ <> ]*)+)*(?(Open)(?!)) </table>


------解决方案--------------------
如果楼主能保证其它 <table> 都包含在这个 <table> 内,且所有的 <table> 和 </table> 都是配对出现的,那么用这个是最简单的了

<table[^> ]*> [\s\S]* </table>

如果不是这样,即还有不包含在这一 <table> 标签内的其它 <table> 标签,那么
1、此 <table> 标签内就只有 <table> 嵌套标签,而没有其它 <...> 形式的标签,那么可以用楼上所给平衡组,或是如下两种形式


<table[^> ]*> (([^ <> ]*(?: <[^> ]*> )+?[^ <> ]*?)+|[^ <> ]*) </table>

<table[^> ]*> (?> <table[^> ]*> (? <TOPTABLE> )| </table> (? <-TOPTABLE> )|[^ <> ])*(?(TOPTABLE)(?!)) </table>

2、此 <table> 标签内还有其它标签,如 <tr> 等,或是 </table> 可能不配对出现,正则实现起来有些困难,如果楼主确实有此需要,说明一下,我用其它方式来实现