Regex to Match a Canadian Postal Code
Copy the pattern, paste addresses, and watch each Canadian postal code light up. Below: the letter-digit alternating structure.
What this pattern matches
It matches a Canadian postal code in the A1A 1A1 shape: alternating letters and digits, with an optional space in the middle.
Part by part
[ABCEGHJ-NPRSTVXY] — the first letter, restricted to letters actually used in the leading position (D, F, I, O, Q, U, W and Z are excluded).
\d[A-Za-z] — digit then letter.
? — optional space.
\d[A-Za-z]\d — the trailing digit-letter-digit block.
Validation
Anchor with ^…$ to validate a single field, and normalise case or add the (i) flag for lower-case input.
Use it in your code
import re
text = "K1A 0B1, M5V 2T6, H2Z 1A7, V6B4Y8, T5J 3N4"
pattern = r'''[ABCEGHJ-NPRSTVXYabceghj-nprstvxy]\d[A-Za-z] ?\d[A-Za-z]\d'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "K1A 0B1, M5V 2T6, H2Z 1A7, V6B4Y8, T5J 3N4";
const re = new RegExp(String.raw`[ABCEGHJ-NPRSTVXYabceghj-nprstvxy]\d[A-Za-z] ?\d[A-Za-z]\d`, "g");
console.log(text.match(re));
FAQ
Why are some letters excluded from the first position?
Canada Post never uses D, F, I, O, Q, U in the first position, and W and Z are not used there either. The class reflects that.
Is the space required?
No. ? makes it optional, so 'V6B4Y8' and 'V6B 4Y8' both match.
Does it validate that the code is a real, assigned code?
No regex can do that — it only checks the format. Real-existence needs a lookup against Canada Post data.