我创建了一个正则表达式来替换字符串中的子字符串。但是,显然我创建的正则表达式不正确。请指导
我想替换 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
为了更清楚起见,我也在图像中附加了字符串
最佳答案
根据这个answer ,你可以试试
<figure id="attachment_5438".+?(?=</figure>)</figure>
关于java - java中用正则表达式替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188698/