Blog Archives

.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/