Saturday, July 31, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jul 2010 >>
S M T W T F S
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
             

Search

Categories
 • Acrobat (5) [RSS]
 • Adobe (96) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (7) [RSS]
 • AIR (233) [RSS]
 • Appearances (198) [RSS]
 • Books (78) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1409) [RSS]
 • ColdFusion Builder (9) [RSS]
 • Data Services (36) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (248) [RSS]
 • Flex (513) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (119) [RSS]
 • JRun (14) [RSS]
 • Labs (47) [RSS]
 • LiveCycle (35) [RSS]
 • MAX (238) [RSS]
 • Mobile (138) [RSS]
 • Regular Expressions (18) [RSS]
 • RIA (21) [RSS]
 • SQL (42) [RSS]
 • Stuff (543) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (164) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
November 28, 2003

RegEx Patterns to Match Zip Codes and Postal Codes

Need to match zip codes or postal codes? Of course, no two countries use the same format. But here are solutions for USA, Canada, and the United Kingdom.

The USA format is simple, five digits as 99999 or zip+4 as 99999-9999. A simple RegEx could be:

\d{5}(-\d{4})?
If you want to omit zips with a trailing hyphen (as in 99999-) then you could use a lookahead condition:
\d{5}(?(?=-)-\d{4})

Canada is a little trickier, the format looks like A1A 1A1 which can be easily matched with:

[A-Z]\d[A-Z] \d[A-Z]\d

However, there is one rule that may be employed to improve validation, the opening character of the set of characters (technically called the "forward sortation area" or FSA) identifies the province, territory, or region (there are 18 characters that are valid in this position, A for Newfoundland and Labrador, B for Nova Scotia, K, L, N and P for Ontario excluding Toronto which uses M, and so on.), and so validation should ideally check to ensure that the first character is a valid one. And so, here is a better Canadian postal code regular expression:

[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d

Good old UK is the trickiest of the three. United Kingdom postcodes, as defined by the Royal Mail, are five, six, or seven characters and digits (that includes a single space). Postcodes are made up of two parts, the "outward postcode" (or outcode), and the "inward postcode" (or incode). The outcode is one or two alphabetical characters followed by one or two digits, or one or two characters followed by digit and a character. The incode is always a single digit followed by 2 characters (any characters excluding C, I, K, M, O, and V). The incode and outcode are separated by a space. Here's the regular expression:

[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}

If you have any other countries or formats to share, please do so.

Comments
for all things postal i generally look here first:
http://www.upu.int/post_code/en/addressing_formats...

for specific in-country info (i'll bet some countries postal codes vary internally):
http://www.upu.int/post_code/en/list_of_sites_by_c...
# Posted By PaulH | 11/28/03 1:35 PM
The easiest place to get regex for assorted
zips in various countries is to consult the perl module <a href="http://search.cpan.org/perldoc/Regexp::Common::zip...;.
# Posted By Iain | 11/28/03 9:51 PM
Regular Expressions Rock! I've been using them for form validation for years. Here's a killer re that validates dates:

^(?:(?:(?:0[13578]|1[02])([\/|\-|\.]?)31)\1|(?:(?:0[1,3-9]|1[0-2])([\/|\-|\.]?)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)\d{2})$|^(?:02([\/|\-|\.]?)29\3(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0[1-9])|(?:1[0-2]))([\/|\-|\.]?)(?:0[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)\d{2})$

It's probably the most complicated one I have, but it works really well and can be tweaked for different formats.
# Posted By Mike | 12/1/03 9:19 AM
Here are some other good ones:

Phone numbers:
(^[0-9]{10}$)|(^\([0-9]{3}\) ?[0-9]{3}\-[0-9]{4}$)|(^[0-9]{3}([\.\-])([0-9]{3})\4[0-9]{4}$)

SSNs:
(^[0-9]{9}$)|(^[0-9]{3}\-[0-9]{2}\-[0-9]{4}$)

A different approach to emails:
^[\w-\.]+@(\w+[\w-]+\.){0,3}\w+[\w-]+\.[a-zA-Z]{2,4}$

How about even last names:
^[A-Za-z]*[\.\ '\-]?[A-Za-z]*$
# Posted By Mike | 12/1/03 9:22 AM
how about another UK RegExp that isnt so fussy of cahracter cassing..

^[A-Za-z]{1,2}[\d]{1,2}([A-Za-z])?\s?[\d][A-Za-z]{2}$
# Posted By pete | 12/2/03 9:39 AM
Just a quick comment regarding the UK postal code R.E:

[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}

Wont this also match:

AAAN NAA

(A=character)
{N=digit)

which it shouldnt do!!!
# Posted By Martin Garbutt | 2/16/04 11:54 AM
Dutch postcodes are real easy: "NNNN AA" = four digits, a space, and two characters. The numbers range from 1000 to 9999, the characters from "AA" to "ZZ", omitting no characters.

As opposed to many countries' postcodes, the Dutch postcodes are unique per block or at least street. So stating the postcode plus house number refers to a unique house.

Jerry
# Posted By Jerry | 3/10/04 4:09 AM
Regarding Canadian postal codes, you can refine your regexp even more. You are correct in listing the 18 valid characters for the first letter of the postal code. For the other letters, there are 20 valid characters: [ABCEGHJKLMNPRSTVWXYZ].

So the complete expression is:
[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d

For more information about Canadian postal codes or geocoding data, check out our site at http://www.infinitegravity.ca
# Posted By Infinite Gravity Data Services | 10/19/04 1:41 PM
I have added $ to the end of the canadian postal code validation to prevent entry of extra characters:
[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$
# Posted By Alexei | 11/14/06 11:13 AM
A much tighter one for the UK would be (allowing for lower-case characters and one or zero spaces between parts - you should dilute to your own taste):

^ ?(([BEGLMNSWbeglmnsw][0-9][0-9]?)|(([A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9]?)|(([ENWenw][0-9][A-HJKSTUWa-hjkstuw])|([ENWenw][A-HK-Ya-hk-y][0-9][ABEHMNPRVWXYabehmnprvwxy])))) ?[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$

@Martin

That regular expression won't match "AAAN NAA". Though that comment was from over three years ago!
# Posted By Douglas Greenshields | 5/25/07 12:47 PM
hi ppl im not sure how the uk post code works... can anyone enlighten me pls..
i like to match any address in uk with house number 621 and postcode le26un
# Posted By akom | 2/8/09 1:12 PM
Thanks for pointing out the FSA rules.

With some work, I found the following works when there is a space AND no space between the first and second half of the code.

@regexpr(OWNER, "[ABCEGHJKLMNPRSTVXY][0-9][A-Z]( |)[0-9][A-Z][0-9]")
# Posted By Jonathan | 3/10/09 3:32 PM
Ireland has a new postal code system called loc8 (not before time). It is funded by Garmin and not government-accepted yet. However, here's a prototype regex string for it:

^[a-zA-Z][0-9A-Za-z]{2}-[0-9]{2}-[0-9A-Za-z]{3}$
# Posted By Tony Proctor | 7/14/10 6:31 PM
The regex string posted by Tony Proctor is incorrect and should not be used. The Loc8 Code has internal calculations inherent
which dictate some of the characters. For this reason a simple syntax definition (incorrect in itself as shown) is insuffient. Loc8
was designed to satisfy safety critical requirements an therefore basic syntax validation is wholly insuffient and foolhardy. Rights to to
the inherent validation calculations are reserved. If anyone wishes to dicuss use of the correct format;- please contact me directly
Many tks
Gary
# Posted By Gary Delaney | 7/15/10 3:56 PM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved