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

20 responses to “RegEx Patterns to Match Zip Codes and Postal Codes”

  1. PaulH Avatar

    for all things postal i generally look here first:
    for specific in-country info (i’ll bet some countries postal codes vary internally):

  2. Iain Avatar

    The easiest place to get regex for assorted
    zips in various countries is to consult the perl module <a href="">Regexp::Common::zip</a>.

  3. Mike Avatar

    Regular Expressions Rock! I’ve been using them for form validation for years. Here’s a killer re that validates dates:
    It’s probably the most complicated one I have, but it works really well and can be tweaked for different formats.

  4. Mike Avatar

    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}$)
    A different approach to emails:
    How about even last names:
    ^[A-Za-z]*[. ‘-]?[A-Za-z]*$

  5. pete Avatar

    how about another UK RegExp that isnt so fussy of cahracter cassing..

  6. Martin Garbutt Avatar
    Martin Garbutt

    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:
    which it shouldnt do!!!

  7. Jerry Avatar

    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.

  8. Infinite Gravity Data Services Avatar
    Infinite Gravity Data Services

    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:
    For more information about Canadian postal codes or geocoding data, check out our site at

  9. Alexei Avatar

    I have added $ to the end of the canadian postal code validation to prevent entry of extra characters:

  10. Douglas Greenshields Avatar
    Douglas Greenshields

    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}$
    That regular expression won’t match "AAAN NAA". Though that comment was from over three years ago!

  11. akom Avatar

    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

  12. Jonathan Avatar

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

  13. Tony Proctor Avatar
    Tony Proctor

    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:

  14. Gary Delaney Avatar
    Gary Delaney

    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

  15. OB Avatar

    For Canadian it’s a good idea to allow the space between sections to exist or not as users do not always use.

  16. Chris Avatar

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

  17. Chris Avatar

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

  18. Liam Morland Avatar
    Liam Morland

    Some letters are not used in Canadian postal codes, so this is a better regex, which matches all valid codes and no invalid codes:

  19. CF Avatar

    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)

  20. Gene Ramsay Avatar
    Gene Ramsay

    The regex for canada does not include DFIOQU.
    These letters are a problem with scanners

Leave a Reply