0

I have this text

<img src=""https://support.comp.com/inlineimages/1413838988355.png""><br><p style=""MARGIN: 0.0in 0.0in 0.0pt;""><br><span style=""font-size: 10.0pt;font-family: Arial , sans-serif;""></span></p><p style=""MARGIN: 0.0in 0.0in 0.0pt;""><br></p><p style=""MARGIN: 0.0in 0.0in 0.0pt;"">The setup is shown below:</p><p style=""MARGIN: 0.0in 0.0in 0.0pt;"">%as2-From%-%srcfilebase%.%srcfileext%<br></p><p style=""MARGIN: 0.0in 0.0in 0.0pt;""><img src=""/inlineimages/1413839088730.png"">

I need regex to match to: src=""https://support.comp.com/inlineimages/Notify/Oct2014/25899/1413838988355.png"" and src=""/inlineimages/1413839088730.png""

However, when I use the regex img src="".+png"" It matches to:

<img src=""https://support.comp.com/inlineimages/1413838988355.png""><br><p style=""MARGIN: 0.0in 0.0in 0.0pt;""><br><span style=""font-size: 10.0pt;font-family: Arial , sans-serif;""></span></p><p style=""MARGIN: 0.0in 0.0in 0.0pt;""><br></p><p style=""MARGIN: 0.0in 0.0in 0.0pt;"">The setup is shown below:</p><p style=""MARGIN: 0.0in 0.0in 0.0pt;"">%as2-From%-%srcfilebase%.%srcfileext%<br></p><p style=""MARGIN: 0.0in 0.0in 0.0pt;""><img src=""/inlineimages/1413839088730.png"">

Why is it including the stuff between the two hyperlinks? Any help is appreciated.

I'm using regex generator: https://regexr.com/

bogus
  • 457
  • 5
  • 16
  • Your regex is greedy and will try to match as much as possible. Since your entire string starts with `img src=""` and ends with `png""`, it matches it all. – Holden Lewis Sep 18 '19 at 16:18

1 Answers1

2

Change your regex to be non greedy with the use of ?:

img src="".+?png""

Shown here

Nicholas Betsworth
  • 1,707
  • 19
  • 24
  • Works like a charm, thanks! – bogus Sep 18 '19 at 16:20
  • 2
    Just one caveat. If somebody adds any other attributes to the html here (like a class between img and src) this regex approach will break. Generally speaking, regex has problems parsing html, as discussed here https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Chris Belyeu Sep 18 '19 at 16:25