Regex group capturing problem
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开发者_StackOverflow 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?
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>.*)
Please see why you shouldn't be trying this.
Anyway, try (?<imgName>.*?)
instead.
精彩评论