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
  • 1
  • 7
  • 17

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
  • 55,299
  • 7
  • 118
  • 110
2

Please see why you shouldn't be trying this.

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

Community
  • 1
  • 1
strager
  • 86,676
  • 24
  • 133
  • 174