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

这类嵌套正则如何写?通过id属性获得一个标签块
需求:就是通过id获得一个html标签块。

要处理的字符串:
HTML code
<html>
<body>
<div id="div1">
        <div id="div2" style="background:Red;">
                <div id="div3">
                        <table id="table1">
                                <tr>
                                        <td>
                                                <div id="div4" style="width:100p
x"></div>
                                        </td>
                                </tr>
                        </table>
                </div>
        </div>
        <div id="div5">
                <a href="http://www.csdn.net">csdn</a>
        </div>
</div>
<img src="http://www.csdn.net/Images/logo_csdn.gif"/>
</body>
</html>


输入输出样例1
输入:"div1"
输出:
HTML code
<div id="div1">
        <div id="div2" style="background:Red;">
                <div id="div3">
                        <table id="table1">
                                <tr>
                                        <td>
                                                <div id="div4" style="width:100p
x"></div>
                                        </td>
                                </tr>
                        </table>
                </div>
        </div>
        <div id="div5">
                <a href="http://www.csdn.net">csdn</a>
        </div>
</div>


输入输出样例2
输入:"div2"
输出:
HTML code
        <div id="div2" style="background:Red;">
                <div id="div3">
                        <table id="table1">
                                <tr>
                                        <td>
                                                <div id="div4" style="width:100p
x"></div>
                                        </td>
                                </tr>
                        </table>
                </div>
        </div>


输入输出样例3
输入:"div3"
输出:
HTML code
                <div id="div3">
                        <table id="table1">
                                <tr>
                                        <td>
                                                <div id="div4" style="width:100p
x"></div>
                                        </td>
                                </tr>
                        </table>
                </div>



输入输出样例4
输入:"table1"
输出:
HTML code
                        <table id="table1">
                                <tr>
                                        <td>
                                                <div id="div4" style="width:100p
x"></div>
                                        </td>
                                </tr>
                        </table>