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.
Using regular expressions to match a pattern
- Start Visual C#.
- 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 "user" group.
*Capture the substring after the @ symbol and put that into the "host" group.
*Make sure that the first half of the string does not have an @ symbol. - 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 = "johndoe@tempuri.org";
- Check to see if there are any command-line parameters; if there are, retrieve the first parameter and assign it to the variable "s". if ( args.Length > 0 ) {
- 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);
- 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 "user" and "host" named groups within the Groups collection of the Match object.
if ( m.Success ) {
Console.WriteLine("User: " + m.Groups["user"].Value);
Console.WriteLine("Host: " + m.Groups["host"].Value);
} else {
Console.WriteLine(s + " is not a valid email address");
}
Console.WriteLine(); - To keep the console window open after running the application, add the following lines of code:
System.Console.WriteLine("Press Enter to Continue...");
System.Console.ReadLine(); - Build your project.
Regex emailregex = new Regex("(?
s = args[0];
}
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:
On the Project menu, click Properties, and then click Debug. In the Start Options section in the right pane, specify the e-mail address that you want to test. Press F5, or click Start on the Debug menu to run the application.
Available Regular Expression Operators
More Info:
The book Mastering Regular Expressions not only explains everything you want to know and don't want to know about regular expressions, including the regex features that are unique to .NET. It has an excellent chapter on .NET's System.Text.RegularExpressions namespace, explaining the various Regex classes far better than Microsoft's documentatio, with plenty of example VB.NET example code and some C# code showing more advanced techniques.
No comments:
Post a Comment