Canadian Postal Code
Match Canadian postal codes in the A1A 1A1 or A1A1A1 format with valid first-letter prefixes.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("[ABCEGHJ-NPRSTVXY]\\d[A-Z] ?\\d[A-Z]\\d", "gi");
const input = "K1A 0B1";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"[ABCEGHJ-NPRSTVXY]\d[A-Z] ?\d[A-Z]\d", re.IGNORECASE)
input_text = "K1A 0B1"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)[ABCEGHJ-NPRSTVXY]\d[A-Z] ?\d[A-Z]\d`)
input := `K1A 0B1`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
[ABCEGHJ-NPRSTVXY]\d[A-Z] ?\d[A-Z]\d (flags: gi)Raw source: [ABCEGHJ-NPRSTVXY]\d[A-Z] ?\d[A-Z]\d
How it works
Examples
Input
K1A 0B1Matches
K1A 0B1
Input
M5V3L9Matches
M5V3L9
Input
12345No match
—Common use cases
- •Canadian address form validation
- •Shipping label generation
- •Geographic data cleaning
- •E-commerce checkout for CA customers
Related patterns
ISO 3166-1 alpha-2 Country Code
ValidationValidate 2-letter ISO 3166-1 alpha-2 country codes (US, GB, FR, JP, etc.) — structural check only.
ISO 4217 Currency Code
ValidationValidate 3-letter ISO 4217 currency codes (USD, EUR, GBP, JPY, etc.) — structural check only.
SWIFT / BIC Code
ValidationValidate SWIFT/BIC bank identifier codes — 8 chars (head office) or 11 chars (branch).
US ZIP Code
ValidationMatch US ZIP codes in 5-digit (12345) and ZIP+4 (12345-6789) formats.