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

  • Facebook shares see modest debut

    Facebook shares end a volatile first day of trading at $38.23, barely above the company's initial pricing, having initially jumped more than 10%.

  • Met Police to extract phone data

    Mobile phone data of suspects in police custody is to be extracted and retained, regardless of whether charges are brought, the BBC has learned.

  • Silicon trick for next-gen memory

    Researchers reveal details of a promising way to make a fundamentally different kind of computer memory 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...