1

I created a regex to replace a substring from a string. But, apparantly the regex I created is not correct. Please guide

I want to replace a regex from the Html string, but as there are multiple tags like that, it treats them as one tag, it replaces all the tags.

Method used:

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

Following is the Html that in which I want to replace the string

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

For better clarity, I nn have attached the string in the image as well

enter image description here

Fayza Nawaz
  • 2,256
  • 3
  • 26
  • 61
  • 1
    `.*` regex by default is greedy, meaning it takes as many characters as possible. You need to make your regex non-greedy with `.*?`. – Code-Apprentice Oct 01 '19 at 16:23
  • Mandatory link: https://stackoverflow.com/a/1732454. Also please see: [Can you provide some examples of why it is hard to parse XML and HTML with a regex?](https://stackoverflow.com/q/701166), [Using regular expressions to parse HTML: why not?](https://stackoverflow.com/q/590747). – Pshemo Oct 01 '19 at 16:56

1 Answers1

2

According to this answer, you could try

<figure id="attachment_5438".+?(?=</figure>)</figure>
Dennis B.
  • 118
  • 6