C#
  Home arrow C# arrow Page 3 - Working with Regular Expressions in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Working with Regular Expressions in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2008-08-19

    Table of Contents:
  • Working with Regular Expressions in C#
  • Working with RegExes
  • Working with RegExes, continued
  • Lots of Examples
  • Final Words

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with Regular Expressions in C# - Working with RegExes, continued


    (Page 3 of 5 )

    Moving on, you have the Regex.Replace() method. This replaces the occurrences of the specified regular expressions pattern in the input string. There are various variations and usage routes of this because it can be overloaded. The easiest is specifying two parameters: the first is the input string, while the second would be the pattern.

    Check out the following example that changes all of the @’s to [at]’s in the input string.

    string pattern = @"@+";

    Regex rgx = new Regex(pattern);

    string input = stevenpriest@corporation.net;

    string output = rgx.Replace(input, "[at]");

    Console.Write("Modified string: " + output);

    For the entire list of Regex methods just check out MSDN’s documentation. But keep in mind that Regex is a class and it has more than a dozen of methods. Needless to say, the namespace System.Text.RegularExpressions namespace has lots of classes, not only Regex. Each one of them is enumerated over at MSDN here.

    Note: The at (@) sign is used right before the first quotation mark when specifying the regular expressions pattern because we don’t want the C# compiler to interpret the backslash () as an escape character.

    The really important ones are Match (along with MatchCollection), Group (along with GroupCollection), Capture (with its CaptureCollection), and of course,  Regex. The collection ones are to be used when sequences of captured strings are required. These sequences are called collections. For example, you may want to store all of the matches of a pattern, and then you’ll use the MatchCollection. Here’s an example:

    string pattern = @"b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}b";

    MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);

    This example would search for all the email addresses that are valid in terms of the RFC 2822 standards – that is, it can contain any of the A-Z, 0-9, _%+- special characters, and then the same is true for the domain, which ends with a valid A-Z suffix that can be 2, 3, or 4 characters long (such as .tw, .co, .edu, .com, .net, .org, .biz, .mil, .gov, .info, etc.). You can read more about validating email addresses via RegExes here.

    Also, it stores the matches inside the MatchCollection called matches. You can iteratively walk through it now and print out or work with the matches.

    Another way to work with regular expressions is, of course, working directly with regex constructors. We can do this by the following declaration:

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);

    After this point we can simply call the Match or any other method referring to the aforementioned constructor (rgx). Here’s an example of doing this:

    Match mtch = rgx.Match(input);

    Now what if there are more matches and we don’t want to store the results in a MatchCollection? We can iteratively move through the input string like this. Pay attention to the Capture class (herein including its CaptureCollection); that’s how we “capture” the results.

    while (mtch.Success)

    {

    Console.WriteLine("The match: " + mtch);

    CaptureCollection captcoll = mtch.Captures;

    foreach (Capture capt in captcoll)

    {

    Console.WriteLine("The capture: " + capt);

    }

    mtch = mtch.NextMatch(); // move forward and find the next match

    }

    As another example, what if we want to group certain substrings of the matches? Thankfully, there’s the Group class that has representing the results from a single capturing group as its purpose. These groups can capture multiple strings from one match; that’s why there exists a GroupCollection class too.

    Please be aware that the Group inherits from the Capture; that’s how you can refer back directly to the last string that was captured. Concerning how to use them, let’s continue our previous example. Suppose we have a <firstname> and <lastname> delimited as groups in our pattern, such as in the following example:

    string pattern = @"(?<firstname>w+)s+(?<lastname>w+)s*";

    Now with the above pattern, if we extend the previous match and captures example with groups, it would look like this:

    Group fname = mtch.Groups["firstname");

    Group lname = mtch.Groups["lastname");

    Console.WriteLine("The first group: " + fname);

    foreach (Capture capt in fname.Captures)

    {

    Console.WriteLine("The captures of the first group: " + capt);

    }

    foreach (Capture capt in lname.Captures)

    {

    Console.WriteLine("The captures of the second group: " + capt);

    }

    Console.WriteLine("The second group: " + lname);

    Obviously, the continuation from the previous example cannot be left out:

    mtch = mtch.NextMatch();

    Since we don’t want to stop at the first match, I think that the above examples have pretty much presented the way we can work with regular expressions in C#. Now you have the ground work and the basic knowledge to study and learn from MSDN’s documentation about the rest of the classes and methods. Don’t forget that to deepen your knowledge, you also need to practice. So fire up your Visual Studio and have fun!

    More C# Articles
    More By Barzan "Tony" Antal


       · Thanks for reading this article. Hopefully you've found it informative and...
     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT