5.16.2007

More RegEx; US and UK phone numbers and postal codes

Here are some RegEx patterns I've created for parsing and validating US and UK phone numbers and post codes. The "parsing" patterns are designed to work with Microsoft .NET's implementation of RegEx to allow you to pick out the relevant values of the parts of the strings, and will likely not work with other RegEx implementations, such as JavaScript. The "validation" patterns are designed to simply validate whether the string matches the pattern or not; there are no captures made. All of the "validation" patterns should be portable to other RegEx implementations, with the exception of the UK phone number pattern.

US Zip Code parsing pattern:
^(?<Zip>\d{5})[\s-]*(?<Plus4>\d{4})?$
Captures the 5-digit zip code to the named group "Zip" and the optional USPS 4-digit "plus 4" code to the named group "Plus4"

US Zip Code validation pattern:
^\d{5}[\s-]*(?:\d{4})?$

UK Post Code parsing pattern:
^(?<OutCode>(?:[ABD-HJLNP-UW-Z]{1,2})(?:(?:\d{1,2})|(?:\d[ABD-HJLNP-UW-Z])))\s*(?<InCode>\d[ABD-HJLNP-UW-Z]{2})$
Captures the first section of the post code to the named group "OutCode" and the second section of the post code to the named group "InCode".

UK Post Code validation pattern:
^(?:[ABD-HJLNP-UW-Z]{1,2})(?:(?:\d{1,2})|(?:\d[ABD-HJLNP-UW-Z]))\s*\d[ABD-HJLNP-UW-Z]{2}$

Valid UK postal codes follow the below patterns:
A9 9AA
A99 9AA
A9A 9AA
AA9 9AA
AA99 9AA
AA9A 9AA
Source: http://en.wikipedia.org/wiki/UK_postcodes

US Phone Number parsing pattern:
^[\s(]*(?<AreaCode>\d{3})[\s-)]*(?<TownExchange>\d{3})[\s-]*(?<Number>\d{4})$
Captures the area code to the named group "AreaCode", the next 3 numbers to the named group "TownExchange" and the final 4 numbers to the named group "Number".

US Phone Number validation pattern:
^[\s(]*\d{3}[\s-)]*\d{3}[\s-]*\d{4}$

UK Phone Number parsing pattern:
^[\s(]*(?<AreaCode>(?:(?<ac01x1>01\d1)|(?<ac011x>011\d)|(?<ac02x>02\d)|(?<ac01xxxa>01\d{3})|(?<ac01xxxb>01\d{3})|(?<ac01xxxx>01\d{4})))[\s\-)]*(?<TownExchange>(?(ac01x1)\d{3})(?(ac011x)\d{3})(?(ac02x)\d{4})(?(ac01xxxa)\d{3})(?(ac01xxxb)\d{5})(?(ac01xxxx)\d{4,5}))[\s\-]*(?<Number>(?(ac01x1)\d{4})(?(ac011x)\d{4})(?(ac02x)\d{4})(?(ac01xxxa)\d{3}))$
Captures the first set of digits to the named group "AreaCode", the second set of digits to the named group "TownExchange", and the final set of numbers, if it exists, to the named group "Number".

UK Phone Number validation pattern:
^[\s(]*(?:(?<ac01x1>01\d1)|(?<ac011x>011\d)|(?<ac02x>02\d)|(?<ac01xxxa>01\d{3})|(?<ac01xxxb>01\d{3})|(?<ac01xxxx>01\d{4}))[\s\-)]*(?(ac01x1)\d{3})(?(ac011x)\d{3})(?(ac02x)\d{4})(?(ac01xxxa)\d{3})(?(ac01xxxb)\d{5})(?(ac01xxxx)\d{4,5})[\s\-]*(?(ac01x1)\d{4})(?(ac011x)\d{4})(?(ac02x)\d{4})(?(ac01xxxa)\d{3})$

These patterns match the following UK phone number patterns:
(01x1) xxx xxxx (matched by ac01x1)
(011x) xxx xxxx (matched by ac011x)
(02x) xxxx xxxx (matched by ac02x)
(01xxx) xxx xxx (matched by ac01xxxa)
(01xxx) xxxxx (matched by ac01xxxb)
(01xxxx) xxxxx (matched by ac01xxxx)
(01xxxx) xxxx (matched by ac01xxxx)

Source: http://en.wikipedia.org/wiki/UK_telephone_numbering_plan

Labels: , , , , ,


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]