Night | Snow

Regular Expressions in C#

Posted on: December 11th, 2009

Tags: , , ,

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):

public static String GetInnerHtml(String content, String tag, String attr)
{
  Regex re = new Regex(@"(<" + tag + " " + attr + "(.*?)>).*?((?<TAG><" + tag + ").*?(?<-TAG></" + tag + ">))?(?(TAG)(?!))</" + tag + ">");
  return re.Match(content).ToString();
}

Returns all Email-Addresses:

public static String[] GetEmailAddresses(String content)
{
  Regex re = new Regex(@"[\w\.-]{1,}@[\w\.-]{2,}\.\w{2,3}");
  MatchCollection emails = re.Matches(content);
  String[] s = new string[emails.Count];
  for (int x = 0; x < emails.Count; x++)
    s[x] = emails[x].ToString();
  return s;
}

Removes all tags:

public static String RemoveTags(String content)
{
  Regex re = new Regex("(<[^>]*>)");
  content = re.Replace(content, "");
  return content;
}

Returns the title tag:

public static String GetTitle(String content){
  Regex re = new Regex(@"(?<=<title.*>)([\s\S]*)(?=</title>)");
  return re.Match(content).ToString();
}

News

  • Apple lays App Store rules bare

    Apple says that it will publish the guidelines it uses to determine which programs it sells in its App Store to appease critical developers.

  • Google unveils 'instant' searches

    Google speeds up its internet search engine by launching a new product called Instant that displays results as soon as users type in queries.

  • Smartphone chip battle heats up

    Intel is to launch its first chip with built-in graphics, while established phone chipmaker ARM releases a fast new chip.

Martin Fischer said:

Client and partner of simra.ch

Wir können Simon Schärer als kompetenter und zuverlässiger Programmierer höchst weiterempfehlen. Er ist nicht nur ein guter Programmierer, sondern auch ein ausgezeichneter Designer.
Danke Developer’s Island

Follow us on Twitter

favorite

I get asked a lot - which SDK should we use for web programming? I recommend Netbeans

my toolbar

Simra GameMP3 playersubscribe to RSSMy delicious bookmarksFacebookTwitterE-mail

No Responses to “Regular Expressions in C#”

RSS Feed Icon  Subscribe to comments follow-up

Trackbacks/Pingbacks

Leave a reply

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Latest Tweet

loading...