Extract image URL in a string (RSS with syndicatio

2019-09-29 06:56发布

问题:

i have this :

<img src="http://MyUrl.JPG.jpg" width="180" ...

and i need this :

http://MyUrl.JPG.jpg

thank

回答1:

Complete solution with regex :

string source ="<img src=\"http://MyUrl.JPG.jpg\"";
var reg = new Regex("src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))(?:\"|\')?");
var match=reg.Match(source);
if(match.Success)
{
  var encod = match.Groups["imgSrc"].Value;
}


回答2:

If that's really all you have you might get away with a regular expression, like

src="([^"]+)

However, you can't and shouldn't try to parse HTML in general with regular expressions. Using regular expressions to parse HTML: why not?

Instead use an HTML parser like Html Agility Pack. I don't know if it's available for WP7, though.