我有一个像这样的长字符串。
<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> </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> </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> </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/