Postal Codes


Sometimes a a programmer you come across the need to work with postal codes in addresses.

Wikipedia says: “A postal code (also known locally in various English-speaking countries throughout the world as a postcode, post code, Eircode, PIN code or ZIP code) is a series of letters or digits or both, sometimes including spaces or punctuation, included in a postal address for the purpose of sorting mail. In February 2005, 117 of the 190 member countries of the Universal Postal Union had postal code systems.”

Postal codes can be numeric or alpha-numeric. For numeric codes, the can range from 3 digits to 10 digits. For alpha-numeric codes they can range from a length of 6 to 8 digits.

Wikipedia later in their article says: “Postal codes in Canada do not include the letters D, F, I, O, Q, or U, as the optical character recognition (OCR) equipment used in automated sorting could easily confuse them with other letters and digits. The letters W and Z are used, but are not currently used as the first letter. The Canadian Postal Codes use alternate letters and numbers (with a space after the 3rd character) in this format: A9A 9A9.” There are 20 possible letters.

Given the above rules for Canadian postal codes, what regular expression (regex) could be used to validate a Canadian postal code?

[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]

If you want an optional space in the middle, try this.

[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]