首页 > 解决方案 > 在java中替换字符串的正则表达式

问题描述

我创建了一个正则表达式来替换字符串中的子字符串。但是,显然我创建的正则表达式是不正确的。请指导

我想从 Html 字符串中替换一个正则表达式,但是由于有多个这样的标签,它将它们视为一个标签,它会替换所有标签。

使用方法:

pattern = <figure id="attachment_5438".*</figure>
html = html.replaceFirst(pattern, "text to replace");

以下是我要替换字符串的Html

Temp
<figure id="attachment_5438" align="aligncenter" width="1832"><img ng-test class="size-full wp-image-5438" src="link/wp-content/uploads/2019/05/ATA070219portfolio_img02.jpg" alt="California red-sided garter snake (Thamnophis sirtalis infernalis) photographed at the Cheyenne Mountain Zoo in Colorado Springs, Colorado." width="1832" height="1374" /> California red-sided garter snake (<em>Thamnophis sirtalis infernalis</em>) photographed at the Cheyenne Mountain Zoo in Colorado Springs, Colorado.</figure>  <figure id="attachment_5439" align="aligncenter" width="1832"><img ng-test class="size-full wp-image-5439" src="link
 /wp-content/uploads/2019/05/ATA070219portfolio_img03.jpg" alt="Spanish shawl nudibranch (Flabellinopsis iodinea) photographed at the Research Experience and Education Facility, or REEF, at UC Santa Barbara in Santa Barbara, California." width="1832" height="1374" /> Spanish shawl nudibranch (<em>Flabellinopsis iodinea</em>) photographed at the Research Experience and Education Facility, or REEF, at UC Santa Barbara in Santa Barbara, California.</figure> abc

为了更清楚起见,我还附上了图像中的字符串

在此处输入图像描述

标签: javaregexstring

解决方案


根据这个答案,你可以试试

<figure id="attachment_5438".+?(?=</figure>)</figure>

推荐阅读