Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Last active June 25, 2023 07:56
Show Gist options
  • Save MarketingPip/8e7deae43d98cde63b6716c683c95386 to your computer and use it in GitHub Desktop.
Save MarketingPip/8e7deae43d98cde63b6716c683c95386 to your computer and use it in GitHub Desktop.
Regular Expression for finding a Canada Postal Code in a string.

Regex for Canada Postal Codes

This regular expression will find Canada Postal Code in a string.

[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d

Canada uses ANA NAN format.

From Wiki:

The system was gradually introduced starting in April 1971 in Ottawa.
The letters D, F, I, O, Q, and U are not used to avoid confusion with other letters or numbers.

From Goverment of Canada:

The postal code is a six-character code defined and maintained by Canada Post Corporation (CPC) for the purpose of sorting and delivering mail.
The characters are arranged in the form 'ANA NAN', where 'A' represents an alphabetic character and 'N' represents a numeric character ( e.g. , K1A 0T6).

References

Canada Gov

Wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment