Microsoft KB Archive/308252

= How to match a pattern by using regular expressions and Visual C# 2005 or Visual C# .NET =

Article ID: 308252

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q308252





For a Microsoft Visual Basic .NET version of this article, see 301264.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Text.RegularExpressions



SUMMARY
This step-by-step article shows you how to create and use regular expressions to determine whether strings match certain patterns. Regular expressions allow for easy parsing and matching of strings to a specific pattern. Using the objects available in the RegularExpressions namespace, you can compare a string against a given pattern, replace a string pattern with another string, or retrieve only portions of a formatted string. In this example, we will construct a pattern to validate a e-mail address.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual C# 2005 or Microsoft Visual C# .NET

This article assumes that you are familiar with the following topics:
 * Visual C# 2005 or Visual C# .NET
 * Regular expression syntax

Using regular expressions to match a pattern
 Start Visual C# 2005 or Visual C# .NET. Create a new Visual C# Console Application.  Specify the using keyword on the Text.RegularExpressions namespace so that you will not be required to qualify declarations in those namespaces later in your code. The using statement must be used prior to any other declarations: using System.Text.RegularExpressions;   Define a new regular expression that will use a pattern match to validate an e-mail address. The following regular expression is structured to accomplish three things:  Capture the substring before the @ symbol and put that into the &quot;user&quot; group. Capture the substring after the @ symbol and put that into the &quot;host&quot; group.</li> Make sure that the first half of the string does not have an @ symbol.</li></ol>

Regex emailregex = new Regex(&quot;(? [^@]+)@(? .+)&quot;); </li>  Define a new string containing a valid e-mail address. This provides a default value if the method's command-line argument is empty: String s = &quot;johndoe@tempuri.org&quot;; </li>  Check to see if there are any command-line parameters; if there are, retrieve the first parameter and assign it to the variable &quot;s&quot;. if ( args.Length > 0 ) { s = args[0]; }                   </li>  Use the Match method to pass in the e-mail address variable and return a new Match object. The Match object will return regardless of whether any matches were found in the source string. Match m = emailregex.Match(s); </li>  By examining the Success property, we can decide whether to continue processing the Match object or to print an error message. If successful, display the &quot;user&quot; and &quot;host&quot; named groups within the Groups collection of the Match object. if ( m.Success ) { Console.WriteLine(&quot;User: &quot; + m.Groups[&quot;user&quot;].Value); Console.WriteLine(&quot;Host: &quot; + m.Groups[&quot;host&quot;].Value); } else { Console.WriteLine(s + &quot; is not a valid email address&quot;); } Console.WriteLine; </li>  To keep the console window open after running the application, add the following lines of code: System.Console.WriteLine(&quot;Press Enter to Continue...&quot;); System.Console.ReadLine; </li> Build your project.</li> To run the application in the development environment using the default e-mail address specified in the code, press F5 or select Start from the Debug menu. To start the application with a command-line argument, there are three options: <ul> On the Project menu, click Properties. In the left pane, click the Configuration Properties folder, and then click Debugging. Under Start Options, click Command Line Arguments and specify the e-mail adress that you want to test. Press F5 or select Start from the Debug menu to run the application.</li> Start a command window and navigate to the &quot;bin\debug&quot; folder under the folder in which your project resides. Then type in the name of the executable followed by the e-mail address you wish to test.</li> Locate the executable file for this project, and drag it to the Start...Run window on the taskbar. Add the e-mail address to verify, and click or press OK.</li></ul> </li></ol>

<div class="references_section">