Click here to Skip to main content
12,813,994 members (33,178 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 16 Jul 2010

Using Regular Expressions in C# .NET

, 16 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Explains regular expressions, and some of the common ways to use them.


Developers may need to compare and search strings in their code. Regular expressions are an older but valuable technique at the Developer’s disposal. Although the .NET Framework has in-built methods to search and sort strings, regular expressions should still be understood to ensure that code development is efficient. In this article, Anupam Banerji explains regular expressions, and some of the common ways to use them.


The older computer languages were programmed with severe hardware restrictions. Memory usage was a key factor between competing algorithms. The algorithm with the smallest memory requirement would then be considered optimal, even if it were slower. PERL developers therefore created regular expressions to compare, search and modify strings. Regular expressions are a powerful, albeit complex method of manipulating large quantities of strings.

The memory usage problem has slowly disappeared. Memory these days is cheap, abundant and increases in capacity almost exponentially every few years. The efficiency of the algorithm is more important today than the amount of memory it consumes. Regular expressions are therefore still a valuable (although complex) method of writing good code.

Creating Regular Expressions

Regular expressions are an efficient way to process text. The following regular expression looks complicated to a beginner:


The PERL developer would smile. All this regular expression does is return the exact same word entered that the expression is compared to. The symbols look very difficult to understand, and are. The ^ symbol refers to the start of the string. The $ refers to the end of the string. The \w refers to the a whole word with the characters A-Z, a-z, 0-9 and underscore. The + is simply 0 or more repetitions. The regular expression would match:


Using Regular Expressions in C# .NET

The System.Text.RegularExpressions namespace contains the Regex class used to form and evaluate regular expressions. The Regex class contains static methods used to compare regular expressions against strings. The Regex class uses the IsMatch() static method to compare a string with a regular expression.

bool match = Regex.IsMatch
(string input, string pattern);

If writing C# code, the example above would be:

if (Regex.IsMatch("testtest", @"^\w+$"))
     // Do something here

Another useful static method is Match(), which returns a Match object with all matches in the input string. This is useful when more than one match exists in the input text. The following code results in more than one match:

string text = "first second";
string reg = @"^([\w]+) ([\w]+)$";

Match m = Regex.Match(text, reg, RegexOptions.CultureInvariant);

foreach (Group g in m.Groups)

The expression groups are entered in parentheses. The example above returns three groups; the entire text as the first match, the first word, and the second word. Expression groups are useful when text needs to broken down and grouped into several pieces of related text for storage or further manipulation.

A Quick Example

In this example, we validate an email address using regular expressions. My regular expression works:


However, this isn’t the only expression used to validate email addresses. There are at least two other ways that I have come across. There are many more.

We write a small C# console application that takes some text as an input, and determines if the text is an email address.

using System.Text;
using System.Text.RegularExpressions;

string text = Console.ReadLine();
string reg = @"^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$";

if (Regex.IsMatch(text, reg))
     Console.WriteLine("Not email."); 

Try this with a few real and fake email addresses and see if it works. Let me know if you find an error.


Regular expressions are developed differently. The same task can be accomplished using many different expressions. Expressions created by a developer may be undecipherable by another.

This is why documenting regular expressions is a very important part of the development process. The expression code comments often span several lines, and is worth the effort in case your expression has unintended effects, or if another developer takes over your code. Enforcing good documentation standards for regular expressions will ensure that maintenance issues are minimal.

For example, if we document the regular expression for validating email addresses above, we would write comments like these:

// Validating email addresses
// The expression has three expression 
// groups.
// 1. ((([\w]+\.[\w]+)+)|([\w]+))
// The LHS of the or clause states
// that there may be more than one
// sequence of two words with a .
// between them.
// The RHS of the or clause states
// that there may be a single word.
// 2. (([\w]+\.)+)
// This expression states that there
// may be as many
// words separated by a . between them
// as necessary.
// 3. ([A-Za-z]{1,3})
// This expression states that the
// last set of characters may be upper
// or lowercase letters. There must be
// a minimum of 1 and a maximum of 3.

This may be considered a long set of comments for a lot of development standards, but the expression has been broken down into expression groups. A new developer has very little difficulty in understanding the function and motivation behind writing the expression. This practice should be consistently enforced to avoid headaches when upgrading or debugging software.

Useful Regex Software

If you’ve used a shell script in *NIX, then you’ve used grep. Windows has the PowerGrep tool, which is similar to grep. PowerShell is a another tool which is built on the .NET Regular Expression engine, and has command line scripting utilities. Espresso by UltraPico ( is a free Regular Expression Editor which you can use to build and test your regular expressions.


Regular expressions are an efficient way to search, identify and validate large quantities of text without having to write any comparisons. Although they may be complicated, writing and documenting regular expressions allows the developer to concentrate on more important parts of the implementation process. The use of several free and open source regular expression tools makes understanding and building regular expressions a worthwhile task.

To download this technical article in PDF format, go to the Coactum Solutions website at




About the Author

Anupam Banerji
Australia Australia


You may also be interested in...


Comments and Discussions

Suggestion"The + is simply 0 or more repetitions" ??? Pin
ACommonMan12-Nov-15 23:29
memberACommonMan12-Nov-15 23:29 
Questiononline regex tester Pin
grunger_net12-Jul-15 6:31
membergrunger_net12-Jul-15 6:31 
General[My vote of 1] A taste of honey only Pin
Gustav Brock24-Jun-15 0:30
memberGustav Brock24-Jun-15 0:30 
QuestionFound a hole with this theory Pin
joshrduncan201212-Jul-13 11:52
memberjoshrduncan201212-Jul-13 11:52 
GeneralMy vote of 4 Pin
Abhijeet Shetkar5-Apr-13 1:14
memberAbhijeet Shetkar5-Apr-13 1:14 
Suggestion7 Regular Expressions a C# Developer Must Know Pin
Coddioz11-Nov-12 23:26
memberCoddioz11-Nov-12 23:26 
GeneralRe: 7 Regular Expressions a C# Developer Must Know Pin
Anupam Banerji20-Nov-12 5:07
memberAnupam Banerji20-Nov-12 5:07 
Question[My vote of 1] Poor Pin
CIDev6-Sep-12 4:48
memberCIDev6-Sep-12 4:48 
This is too short to be an article and no program is included. You do not describe enough about the syntax of regular expressions for readers to actually make their own. You give an ok description of what Regular Expressions are, but this 'article' is more like an introduction to an article rather than a full blown article itself.

GeneralMy vote of 3 Pin
Satwadhir Pawar16-Feb-11 22:01
memberSatwadhir Pawar16-Feb-11 22:01 
GeneralMy vote of 1 Pin
Muhammad Tufail 19794-Jan-11 23:39
memberMuhammad Tufail 19794-Jan-11 23:39 
GeneralMy vote of 1 Pin
kobymeir17-Jul-10 2:16
memberkobymeir17-Jul-10 2:16 
GeneralExplanation required Pin
Md. Marufuzzaman14-Jul-10 2:49
mvpMd. Marufuzzaman14-Jul-10 2:49 
GeneralRe: Explanation required Pin
Anupam Banerji14-Jul-10 2:53
memberAnupam Banerji14-Jul-10 2:53 
GeneralRe: Explanation required Pin
Md. Marufuzzaman14-Jul-10 4:42
mvpMd. Marufuzzaman14-Jul-10 4:42 
GeneralMy vote of 2 Pin
Trollslayer13-Jul-10 23:14
mentorTrollslayer13-Jul-10 23:14 
GeneralRe: My vote of 2 Pin
GrubbyHalo4-Sep-13 0:42
memberGrubbyHalo4-Sep-13 0:42 



Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 16 Jul 2010
Article Copyright 2010 by Anupam Banerji
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid