Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, April 10, 2007

Custom Regular Expressions with Examples

This Article explains how can we use the custom regular
expressions in .NET. Here nearly 50 custom regular expressions covered. after
reading this article you should be able to create any type of custom reqular

Here is the .NET Custom regular expressions list.

Matches hello there, hello sam, hellotopical

To match the end
of the string, use the $ character. For example:

Matches Where, and There

The ^
and $ characters are know as "Atomic Zero Width Assertions",
in case you were wondering.

Character Classes

Character classes
allow you to specify sets of characters or ranges. For example:

Matches Hey, and Hi, but not Zzz

In other words,
the string must contain at least one of the characters in the character class.
You can also exclude characters. For example:

Matches Zzz, but not Hey or Hi.

When the ^
character is the first character in the character class, it means "anything
but the following characters".

Putting this together,
we could create a pattern that matches strings that start with a vowel:


Or, strings that
don't start with a vowel:


With character
classes, you can also specify ranges. For example:

Matches 0, 5, 8, or any number between 0 and 9.

Matches any two digit number (04, 13, 87, etc.), but there's
a better way to do this.

Matches characters typically found in words. A short hand syntax
for this is \w

Some other build
in classes are \W for anything other than a word character
([^a-zA-Z0-9_]). \s for any whitespace character.
\S for any non-whitespace character. \d
for any decimal ([0-9]) and \D for any non-decimal


Sometime you want
to specify a certain number of characters that match a certain pattern.
For example, a zip code is 5 digits. This is written as:

Which says match the beginning of the string, followed by five digits, followed
by the end of the string. This matches 97211, 01293, 88460.

Matches 9 digit zip codes, such as 97211-0165.
This could also be written as ^\d{5}-\d{4}$. where \d
matches any decimal (the same as [0-9]).

You can also specify
minimum and maximum
style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">occurrences.
For example:

matches 1, 15, 987. In other words, any number that
is 1 to 3 digits in length.

You can specify
open ended ranges:

matches 1 or more digits. This can also be written ^\d+$

matches 0 or more digits. This can also be written ^\d*$

matches 123, +123, and -123.
This could also be written as ^[+-]?\d* where ?
means 0 or 1 occurrences.


The regular exPssion
syntax contains a number of options that you can toggle. For example,
you can enable case insensitive matching with (?i:):

matches hello, and HELLO, but not


US Phone Number:

matches (555) 555-5555, or 555-555-5555

Improved US Phone Number

This recognizes 1-123-456-7890, 1 (123) 456 7980, 1 123 456
7890, (123) 456-7890, 123-456-7890, and so on, and makes sure that if one
paren is Psent both must be Psent.

International Phone

matches 1-12-3123-4141.

E-Mail Address
(by Lucadean)


5 Digit Zipcode

matches 12879, 97211

9 Digit Zipcode

matches 97211-1234

5 or 9 Digit Zipcode
This exPssion will match 12345, 123451234, or 12345-1234.

(as in MM-DD-YYYY or MM/DD/YYYY, by Chow). Accepts 1 or 2 digits for month
and day.


More sophisticated
date, that accepts dates from 1/1/0001 - 12/31/9999 (mm/dd/yyyy), and validates
leap years (2/29/2000 is valid, but 2/29/2001 is not) - By Mike Akins based
off work by Michael Ash


Here's a version of the above
date exPssions that matches UK dates (dd/mm/yyyy) - by Adam Carless


IP Address

matches, and, but
doesn't match or 999.1.1.1.

Make sure a string
doesn't contain certain characters (by Chris Venus):

matches hello, eye, fred (any string that doesn't have "a"
or "b" in it), but doesn't match bye.

UK Postal Codes
by John Dyke.

Their format
is an outer part: 1 or 2 letter(s) + 1 or 2 digits + a letter (sometime mainly
London) an inner part 1 digit and two letters.

The code is normally
written in capital letters with a space between the outer and inner parts;
it is understandable if the space is omitted

This regular
exPssion validates upper or lower case with or without the space:

CF1 2AA matches
as does cf564fg (= CF56 4FG) but a1234d, A12 77Y would not.

Extract all the
HTML tags from a web page:

In conjunction
with a little .NET code that extracts all the matches, this can be used to
extract every HTML tag from a page.


Or, if you just
want image tags, for example:


To get the values
of a CSV (updated by Arnold Bailey), you can use this exPssion:

In conjunction
with this code:
Regex r = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
string s = "\"a\",b,\"c, d, e\",,f";
string[] sAry = r.Split(s);
for(int i=0;i <; sAry.Length;i++)

Percentage (by
Andres Garcia)

- Matches 0, 0.0, 99.9, 100.0, but excludes -1, 100.1, etc.
File Names (by Karl Moore)
^([a-zA-Z]\:\\)\\([^\\]+\\)*[^\/:*?"<;>;]+\.DOC(l)?$ - alter the DOC here to your "valid" file extension, use "IgnoreCase"

Sample matches: c:\data.doc, e:\whitecliff\staff\km\file.DOC, \\network\km\file.doc

Sample nonmatches: c:\, c:\myreport.txt, sitrep.doc

Sample VB.NET code:

Dim blnMatch As Boolean, strValue As String = "c:\files\report.doc"
blnMatch = System.Text.RegularExPssions.Regex.IsMatch( _
strValue, "^([a-zA-Z]\:\\)\\([^\\]+\\)*[^\/:*?""<;>;]+\.doc(l)?$", _

Credit Card Numbers (by Sushrut Joshi)
^\d{4}((-\d{4}){3}$)((\d{4}){3}$) - Matches 1234-1234-1234-1234 or 1234123412341234, but not 1234-123412341234.

Numeric Value (by Benoit Aubuchon)
^([^.][-0-9.]+[^.-])$ This will match 0.9, -99 but not .3, 4. and 4-

Post a Comment