Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

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

}

Thursday, 24 July 2008

REGEX to remove HTML formatting

I've seen a few ways of using Regex to remove HTML formatting, but the following seems to work fine for me!:

public string Strip(string text)
{
string sReturn = System.Text.RegularExpressions.Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
sReturn = System.Text.RegularExpressions.Regex.Replace(text, @"(&\w*;)", string.Empty);
return sReturn;
}