Regular Expressions in C#
Posted on: December 11th, 2009
Tags: innerhtml, nested tags, Regex, RegularExpressions
A regular expression is a pattern of text that describes what to match in a string. It serves as a filter to find what you’re looking for. The two main function of using regular expression is to match and to replace. The former determines if the pattern is in the string, and if so, find it. Replace changes the string according to the pattern to another pattern.
You can use regular expression (Regex) in your C# (or any other .NET language) application by importing the namespace System.Text.RegularExpressions.
The following examples show the usage of Regular Expressions in C#. I needed them in one of my project to find stuff on a website and I find them very useful.
Returns the innerHtml of a tag (i.e. div) with certain attributes (works with nested tags):
{
Regex re = new Regex(@"(<" + tag + " " + attr + "(.*?)>).*?((?<TAG><" + tag + ").*?(?<-TAG></" + tag + ">))?(?(TAG)(?!))</" + tag + ">");
return re.Match(content).ToString();
}
Returns all Email-Addresses:
Removes all tags:
{
Regex re = new Regex("(<[^>]*>)");
content = re.Replace(content, "");
return content;
}
Returns the title tag:
Regex re = new Regex(@"(?<=<title.*>)([\s\S]*)(?=</title>)");
return re.Match(content).ToString();
}
Posted in C#,Code Snippets | Trackback Url








No Responses to “Regular Expressions in C#”
Trackbacks/Pingbacks
Leave a reply