提取除Java中包含HTML表的字符串以外的所有字符串数据

问题描述 投票:0回答:2

我有一个很长的字符串,像这样。

<p>Some Text above the tabular data. I hope this text will be seen.</p>

<table border="1" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td style="width:150px">
            <p>S.No.</p>
            </td>



            </td>
        </tr>
        <tr>
            <td style="width:150px">
            <p>2</p>
            </td>


    </tbody>
</table>

<p>&nbsp;</p>

<p>Please go through this tabular data.</p>

<table border="1" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td style="width:150px">
            <p>S.No.</p>
            </td>


        </tr>
        <tr>
            <td style="width:150px">
            <p>1</p>
            </td>


        <tr>
            <td style="width:150px">
            >
            </td>

            </td>
        </tr>
    </tbody>
</table>


<p>End Of String</p>

现在,我想像这样在html表之前和之后提取整个字符串。并在HTML Table中添加“ HTML Table ...”。我尝试了一些尝试,但未能实现。尝试拆分成数组,但是没有用

样本输出

<p>Some Text above the tabular data. I hope this text will be seen.</p>

<p>&nbsp;</p>
HTML Table.... 
<p>Please go through this tabular data.</p>


<p>End Of String</p>
java string string-matching
2个回答
0
投票

您可以使用正则表达式处理多行且不区分大小写的标志String.replaceAll,只需使用(?is)即可完成此操作]

String noTables = longTableString.replaceAll("(?is)(\\<table .*?\\</table\\>)", "HTML Table...");
// result
<p>Some Text above the tabular data. I hope this text will be seen.</p>

HTML Table...

<p>&nbsp;</p>

<p>Please go through this tabular data.</p>

HTML Table...


<p>End Of String</p>


0
投票

这可能不是最优雅的解决方案,您可以从使用正则表达式开始捕获表位置,然后将其替换为所需的内容。如下所示会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.