![]() |
|
|
asp:Feature
Introduction to Regular Expressions: Part I Creating Expressions
This two-part article series provides a quick and practical introduction to using regular expressions. Regular expressions can be used for many things; however, they are typically used for input validation or to perform advanced searches on text in supporting applications. This first article will explain how to create a regular expression pattern; the expression defines what is considered a match. The second article will provide details on how to implement regular expressions in .NET applications.
Before starting Id like to point out I have a free regular expression tester available on my Web site (http://www.mitchelsellers.com); you can use this to test the behavior of your regular expressions. During the second article Ill discuss the specific options available on this test page, as well as how the page was created.
Regular expressions have three basic types of symbols that are used: meta characters, escape characters, and character classes. The following table lists the important meta character(s), a short description, and an example of each.
The characters in the table below are used to match special characters in regular expressions; we will use some of these later in this article. NOTE: This is a list of commonly used escape characters, not a complete list of escape characters.
Escape Characters
Below are character classes that represent different groups of characters to make it easier to match common groups of characters.
Character Classes
How to Apply this Information Now that weve explained the various characters included in matching regular expressions, lets walk through some practical examples to illustrate how all these items are pulled together. In the following subsections Ill walk you through a series of real-world validations and provide examples with detailed information.
Before beginning the examples I want to point out that in ALL of my examples the regular expressions created start with the ^ character and end with the $ character. This is done to ensure that the expression matches the entire string. This is done to ensure that the string is that match, and ONLY that match. Otherwise, you can receive matches for strings with more than the included characters. You may play around with this using my expression tester to see the effects of omitting the ^ and $ characters.
Postal Code Validation Postal code validation is a very common user input validation; typically, your postal code will either be five digits or nine digits, with a hyphen after the fifth digit. We can validate this input with the following expression:
^\d{5}(-\d{4})?$
First we have the \d{5} portion of the expression, which indicates that the input must start with five digit characters (0-9). Next the portion of the expression inside the parenthesis, -\d{4} indicates a hyphen (-) to be followed by four digit characters. This is grouped within parentheses and has a question mark appended to the end. This question mark indicates that the input should have zero or one of the preceding items, which happens to be the entire expression in the parentheses. Therefore, in the case of zero, the expression would simply be five digit characters; in the case of one, the expression would be five digits, a hyphen, and four more digits.
Simple Date Validation Validation of date input is another very common occurrence, full regular expression date validation is very involved; however, it is very easy to restrict users to a MM/DD/YYYY format with basic checking for incorrect input. Below is a regular expression to validate a date in the MM/DD/YYYY format; Ive added parenthesis for readability:
^([01]\d)/([0-3]\d)/(\d{4})$
The first section of this expression ([01]\d) represents the month portion of our date, because there are only 12 months in the year we restrict the first digit to either a zero or a one, and the second character can be any number 0-9. This is one portion of this example that can be improved upon; you can modify and create regular expressions that are capable of validating that the input is between 1 and 12 (however, this is outside the scope of this article).
The second section of this expression ([0-3]\d) represents the day portion of our date. This is separated from our first part by a / character, which is a literal requirement that the month be separated from the date by a forward slash. The first part of our day check requires that the first digit of the day is a 0, 1, 2, or 3, then the second digit can be any number 0-9. Just as with the month portion, this can be expanded to ensure that the day value is appropriate for the month provided; however, it is outside the scope of this article.
The final section of this expression is again separated by a / character, then it allows for four digit characters to be entered. This forms the final portion of the date.
Phone Number Validation Another common input item to validate are phone numbers, including area codes and extensions. Below is a sample regular expression that validates a phone number that meets one of the following formats; (555) 555-1212, 555-555-1212, (555) 555-1212 x1111, or 555-555-1212 x1111. Portions of the expression have been highlighted to illustrate the different sections of logic. These sections will be explained below:
^ (\(\d{3}\)\s|\d{3}\s) (\d{3}[\s-]\d{4}) (\sx\d+)?$
The yellow portion of this expression validates the area code input. Notice that we have two individual groups separated by the or operator (|). This indicates that one of the two expressions must be true. The first one validates on a left parenthesis (, three digits, a right parenthesis ), and a space; the second option validates on three digits and a space. Therefore, the phone number must begin with either (515) or 515; this validates the area code portion of our phone number.
The green portion of this expression validates the remaining portion of the standard phone number. The first part \d{3} requires three digits, then the [\s-] allows for either a space or a hyphen. This is then followed by the \d{4} portion, which indicates that an additional four digits are required. We now have validation for a standard 10-digit phone number with support for multiple formats.
The gray portion of this expression validates the optional telephone extension. The expression \sx\d+ indicates that the input string should have a space, the letter x, and then one or more digits. This is enclosed in parentheses and followed by a question mark to indicate that it is optional. This provides for validation of numbers such as (555) 555-1212 x102.
This should provide a helpful overview of regular expressions. Stay tuned for Part II.
Mitchel W. Sellers is a Microsoft Certified Professional Developer with multiple specializations. Hes been developing in .NET since shortly after the release of .NET 1.1 He is the Co-Founder of a startup software consulting firm, IowaComputerGurus L.L.P. He is also very active in multiple online communities, including GotDotNet and DotNetNuke. Find out more about him at http://www.mitchelsellers.com or e-mail him at mailto:mitchel.sellers@gmail.com.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||