java - java中用正则表达式替换字符串

标签 java regex string

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

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

使用方法:

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

为了更清楚起见,我也在图像中附加了字符串

enter image description here

最佳答案

根据这个answer ,你可以试试

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

关于java - java中用正则表达式替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188698/

相关文章:

java - dicom 年龄的正则表达式

java - 如何在JAVA中从字符串(字符数组)中提取键值

java - Java中的多个同时子字符串替换

java - 应用程序未在 websphere 8.5.5 中启动,但在 tomcat 中运行良好

java - Hibernate配置文件: Archive for required library persistence. xml无法读取

Java Math.atan() 为某些输入返回 0?

java - 使用正则表达式对 URL 字符串进行编码

Java,惰性表达式的正则表达式性能较差

c# - 将逗号分隔的字符串转换为 int[] 的最简单方法是什么?

java - ElasticSearch 索引存在不工作/不可靠