Tuesday 29 July 2008

Regex to find images in HTML

The other day I wanted to find a way to extract the image urls from some HTML, so I found the following to work quite nicely:


Regex rgxFiles = new Regex(@"[\w/:.]+\.(?:jpg|bmp|gif|png)");
MatchCollection mtcFiles = Regex.Matches(strSourceString,@"[\w/:.]+\.(?:jpg|bmp|gif|png)",RegexOptions.IgnoreCase);

foreach (Match mFile in mtcFiles)
{

//mFile.Value will contain the image address that we can work with

}

No comments:

Post a Comment