0

I have a very long string were I would like to match path of an image. for example

abc <img src="static/img/about-me.png" title="" alt="". My objective is get string static/img/about-me.png

I have a Pattern @"(?i)(src=)([a-z0-9\\/_-])+\.(png|jpg)" which should work fine, but it is failing because I need to add single " after src=. I tried many approaches, but unable to resolve it. Any help will be appreciated.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

3 Answers3

1

If you're looking to extract the path of a img, this solution in the comments should do the trick:

<img\s.*?src=(?:'|")([^'">]+)(?:'|")

Test: https://regex101.com/r/ZihKlh/1/

kinoute
  • 167
  • 2
  • 10
0

It is not a direct solution, but it worked. I replaced src=" to scr=( and used Regex expression and it work, no issue.

0

The problem was that you didn't put a double quote after your source attribute and you forgot to escape forward slash in your expression.

(?i)(src=")([a-z0-9\\\/_-])+\.(png|jpg)" will work.

I suggest you also try out the regex I spun: <img.*?src="([a-z0-9\\\/_-]+)\.(png|jpg|jpeg)" this will be a bit faster than yours and will make sure you only pick actual image tags.