java - 在java中提取除包含HTML表的字符串之外的所有字符串数据

标签 java string string-matching

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

<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>

最佳答案

您可以使用正则表达式处理多行和不区分大小写的标志(?is),通过String.replaceAll简单地完成此操作:

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>

关于java - 在java中提取除包含HTML表的字符串之外的所有字符串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61756868/

相关文章:

java - "Unexpected end of declaration"安卓助手

java - 如何使用java在linux/ubuntu上获取usb设备的绝对路径

java - 如何在 Java 中仅显示字符串几秒钟?

java - 使用正则表达式获取特殊字符之间的文本

SQLite:在启用 FTS4 的表中使用比较运算符进行查询

java - Java 中的 `x > 0` 和 `x > 0.0D` 之间有什么区别吗?

java - log4j2 访问环境属性

Android - 以编程方式将文本从 strings.xml 添加到 TextView

javascript - 替换 jQuery ID

scala - 关闭!!我如何从 Scala 中的字符串中检测类型?