DynDeveloper.com

DynDeveloper.com

Sign Up Now! Log In
steve gray 8/9/2011 7:17:44 AM

Introduction to Regular Expressions: Part I

This article, and all the other .NET development articles, can be found on the .NET Development Menu  

A word of introduction to the 'introduction'. This is from the magazine ASP.NET. If you are reading this and you don't have a subscripton, stop and get one now - it's invaluable.

 I only copy the article here because the information is so good - I don't want to have to waste time looking for it or worry that it will get archived.

 Again - get the mag. http://www.aspnetpro.com/ 

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 I’d 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 I’ll 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.

 

Character

Description

Example

Matches

^

Indicates the start of a string; used to match a specific beginning sequence.

^abc

abc, acb123, abcdefg

$

Indicates end of a string; used to match a specific ending sequence.

abc$

123456789abc, 987abc

.

Any character excluding \n (new line).

a.c

abc, aac, a9c

|

Or operator used to specify one criteria or another.

john|jane

jane, john

*

Zero or more of previous expression.

12c*

12, 12c, 12cc

+

One or more of previous expression.

1a+c

1ac, 1aac

?

Zero or one of previous expression.

12?c

1c, 12c

\

Escape character, used to make any of the special characters (^, $, ., |, *, +, ?, (, [, {, etc...) literal for matching. See next chart for other escape characters.

1\*a

1*a

{....}

Explicit quantifier notation; used to indicate _ occurrences of a character or character class. A comma can be added to provide min/max occurrences.

12a{2}

12aa, 12aa3

[....]

Matches a range of characters; you can provide collections of characters (abcdefg), as well as hyphenated ranges of characters for matching (A-Z).

123[abc]

123a, 123b, 123c

(....)

Groups a portion of the expression; used to group sections for display.

(123){2}

123123

Meta Characters

 

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.

 

Character

Matches

\b

Word boundary; indicates a space or other non-word character to signify the end of a word.

\t

Tab character.

\n

New line character (great for multi-line textboxes).

\(any metacharacter)

Matches the entered meta character. (\* matches *, \$ matches $).

Escape Characters

 

Below are character classes that represent different groups of characters to make it easier to match common groups of characters.

 

Character Class

Description

Example

Matches

.

Matches any character except \n. If Single Line option is enabled, it matches ANY character.

a.c

aac, abc, a1c

[rstlne]

Matches any single character in the provided list.

a[rstlne]

ar, as, al

[^aeiou]

Matches any single character NOT in the provided list.

a[^aeiou]

ab, ad, ah

[0-9a-zA-Z]

Matches any single character in the following ranges (0 through 9, A through Z, and a through z). The hyphen indicates a range element.

123[0-9A-F]

123A, 1234

\w

Matches any word character; in ECMAScript mode this matches [0-9A-Za-z].

123\w

123a, 1234

\W

Matches any NON-word character; in ECMAScript mode this is the same as [^0-9A-Za-z].

123\W

123$, 123-

\s

Matches any whitespace character; in ECMAScript mode this matches spaces, tabs, and new lines.

123\sa

123 a

\S

Matches any NON-whitespace character.

1\Sa

14a, 1ba

\d

Matches any digit character; in ECMAScript mode this matches 0-9.

\d2

12, 32

\D

Matches any NON-digit character; in ECMAScript mode this matches anything that is not 0-9

\D2

a2, b2

Character Classes

 

How to Apply this Information

Now that we’ve explained the various characters included in matching regular expressions, let’s walk through some practical examples to illustrate how all these items are pulled together. In the following subsections I’ll 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; I’ve 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. He’s 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.

 

Version: Unknown or N/A
Section: .NET Development
This thread has been marked as 'Closed' and archived