gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:46:47 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com