java - 在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
为了更清楚起见,我还附上了图像中的字符串
解决方案
根据这个答案,你可以试试
<figure id="attachment_5438".+?(?=</figure>)</figure>
推荐阅读
- python - 在 lambda 函数中使用条件运行 eval
- r - R计算日期范围之间的每月发生频率
- sql-server - 如何在 Excel 文件中获取值并多次重复执行存储过程?
- python - 当矩阵与向量一起存储时,进行无循环矩阵乘法
- javascript - 如何使用 getUserMedia videoConstraints 在 Firefox 中更改相机视频的宽度和高度?
- python - KivyMD BoxLayout:框布局中的 MDButtons 和工具栏问题
- babeljs - 重新导出 - SyntaxError:意外的令牌“导出”
- r - 如何使情节对用户输入做出反应?
- python - 无法在 AWS EMR Spark 应用程序上运行 MapReduce 作业
- python - 如何在滚动时重试 429 错误