0

If I had an html string containing this somewhere in the middle of it:

<img src="http://images.domain.com/Images/hello.jpg" alt="Failed to Load" />

What regex would I use in order to just obtain the name of the image file? i.e. hello.jpg

Currently I am using this:

(?<front>.*<img.*src="http://images.domain.com/Images/)(?<imgName>.*)"(?<end>.*)

However the value that it finds for the imgName group is:

hello.jpg" alt="Failed to Load

Does anyone know how to fix that?

Immanu'el Smith
  • 683
  • 2
  • 8
  • 18

2 Answers2

4

The easiest fix is to have the imgName group match anything except for quotes by changing .* to [^"]*:

(?<front>.*<img.*src="http://images.domain.com/Images/)(?<imgName>[^"]*)"(?<end>.*)
Quartermeister
  • 57,579
  • 7
  • 124
  • 111
2

Please see why you shouldn't be trying this.

Anyway, try (?<imgName>.*?) instead.

Community
  • 1
  • 1
strager
  • 88,763
  • 26
  • 134
  • 176