C#
  Home arrow C# arrow Page 2 - 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


    (Page 2 of 5 )

    Decades ago people in general were getting familiar with the wildcards in the MS-DOS operating system. It wasn’t unusual to see their use pretty frequently in everyday commands such as: “copy *.exe a:” and so forth. This equaled copying all of the executable files (ending in .exe format) to the a: floppy disk drive. Now, regular expressions are much like these nifty wildcards, but they are a lot more powerful.

    You have a lot of flexibility when using regular expressions because you can format practically any sort of pattern if you are familiar with it and know its exact and correct syntax. Surely, you may say that glancing over the table of syntax isn’t that hard; that’s true, but that doesn’t guarantee that you will also learn the little details of their usage. In this article we’re going to use the .NET framework’s RegEx processor in C#.

    Nowadays, working with the regular expressions in .NET languages has been made easy because the assembly is merged into the main framework. Therefore, all you need to do is include the necessary System.Text.RegularExpressions namespace with the help of the “using” statement at the beginning of your application.

    There are a couple of ways to work with regular expressions. Deciding on which route to follow depends only on what you want to accomplish using RegExes. For example, you can use them to check the existence of a specific pattern in a particular source string, or you may want to search and store the matches in a collection, or who knows, you may want to validate the syntax of an e-mail address (anything@blah.net).

    As a result, you not only need to know the syntax of regular expressions to “talk” with the RegEx processor, you also want to know the pre-existing classes in the said namespace. Chances are, you may face situations where you need to code a bit “more” than just matching strings and all that. Surely, memorizing the table of RegExes helps in the case of UNIX system administrators… grep does wonders, you know.

    Okay, I was getting ahead of myself. Let’s get down to work. In this section we’ll create some basic applications that work with Regular Expressions. You will find C# code snippets here. And in the next section I’ll present to you the periodic table of the RegEx syntax; it’s not really periodic… but it looks quite cryptic for beginners at first glance.

    First things first— there’s the Regex.IsMatch() method. It has three parameters: string input, string pattern, and RegexOptions options. This method indicates whether the specified pattern can be found inside the source string. It returns a plain boolean (true or false). The latter parameter is optional but it may be useful in such cases where ignoring the case is required; this can be done using the following RegexOption:

    System.Text.RegularExpressions.RegexOptions.IgnoreCase

    Let’s enumerate a few of the RegexOptions:

    • IgnorePatternWhitespace – removes un-escaped white spaces.
    • IgnoreCase – ignores the case of the input string.
    • CultureInvariant – ignores the culture of the string.
    • Singleline – changes the mode to single-line.
    • RightToLeft – the input string is read from right to left; this is great for some languages.
    • Multiline – changes the mode to multi-line (^ and $ will apply for lines).

    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 6 hosted by Hostway
    Stay green...Green IT