.Net Regular Expression for Common Fields

Regular Expressions are very handy but sometimes can be daunting depending on what you need to validate. I recently needed a regular expression and decided this would be a good ongoing blog to write. I have a few I will add here as time permits.

This one is good for amounts (whole dollar amount, dollar with decimal, dollar amount with both cents digits):
\A(?<Dollars>[0-9]*)(?:(?<WithCents>\.(?<Cents>[0-9][0-9]))|(?<JustDecimal>\.)|(?<NoCents>>))\z

Zip Code:
\A(?<Zip>\d{5})-(?<Plus4>\d{4})\z

US Phone Number (ddd)ddd-dddd:
\((?<AreaCode>\d{3})\)\s*(?<Number>\d{3}(?:-|\s*)\d{4})

A great tool (and a source for more regular expressions) can be found at:
http://www.ultrapico.com/

Posted on May 28, 2009, in .Net, programming, technology and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: