Haw can we add " in Regex Pattern

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.

Upvotes: 0

Views: 161

Answers (3)

Serendipity
Serendipity

Reputation: 1

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.

Upvotes: 0

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

Upvotes: 0

kinoute
kinoute

Reputation: 167

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/

Upvotes: 1

Related Questions