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-Zd]? d[ABD-HJLNP-UW-Z]{2}
If you have any other countries or formats to share, please do so.

20 thoughts

  1. 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([/|-|.]?)293(?:(?:(?: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]|1d|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.

  2. 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]*$

  3. 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}$

  4. Just a quick comment regarding the UK postal code R.E:
    [A-Z]{1,2}d[A-Zd]? d[ABD-HJLNP-UW-Z]{2}
    Wont this also match:
    AAAN NAA
    (A=character)
    {N=digit)
    which it shouldnt do!!!

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

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

  7. I have added $ to the end of the canadian postal code validation to prevent entry of extra characters:
    [ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ] d[ABCEGHJKLMNPRSTVWXYZ]d$

  8. 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!

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

  10. 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]")

  11. 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}$

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

  13. For Canadian it’s a good idea to allow the space between sections to exist or not as users do not always use.
    ^([ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ]) {0,1}(d[ABCEGHJKLMNPRSTVWXYZ]d)$

  14. This is a decent regex that my company (Net Driven) is using for US and Canadian zip/postal code validation. My name links to one of the pages we use it on.
    ^((d{5})|(([ABCEGHJKLMNPRSTVXY]|[abceghjklmnprstvxy])d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])(s|)d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])d))$
    Just US: ^(d{5})$
    Just Canada: ^(([ABCEGHJKLMNPRSTVXY]|[abceghjklmnprstvxy])d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])(s|)d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])d)$
    This checks for the correct characters as well as allowing lower/uppercase and with/out the space.

  15. This is a decent regex that my company (Net Driven) is using for US and Canadian zip/postal code validation. My name links to one of the pages we use it on.
    ^((d{5})|(([ABCEGHJKLMNPRSTVXY]|[abceghjklmnprstvxy])d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])(s|)d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])d))$
    Just US: ^(d{5})$
    Just Canada: ^(([ABCEGHJKLMNPRSTVXY]|[abceghjklmnprstvxy])d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])(s|)d([ABCEGHJKLMNPRSTVWXYZ]|[abceghjklmnprstvwxyz])d)$
    This checks for the correct characters as well as allowing lower/uppercase and with/out the space.

  16. Some letters are not used in Canadian postal codes, so this is a better regex, which matches all valid codes and no invalid codes:
    ^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] [0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$

  17. Russian postal codes: [1-6]d{5} (6 digits, the first of them can not be 0 and (at the moment) is not greater than 6)

Leave a Reply