Contact
Canada Postal Code Regex
Canadian postal codes alternate letters and digits in the form A1A 1A1. Canada Post never uses D, F, I, O, Q or U, and W and Z do not appear as the first letter, so this pattern encodes those rules directly in its character classes. The space in the middle is optional, which matches how people type codes into shipping, billing and store finder forms. Convert input to uppercase before testing. The honest limit is that the pattern cannot tell whether a particular code is assigned, only that it uses valid letters in valid places.
Pattern (JavaScript)
/^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$/Starts at the start of the line/string, then one of the characters ABCEGHJ-NPRSTVXY, then a digit, then one of the characters ABCEGHJ-NPRSTV-Z, then a space, optionally (zero or one time), then a digit, then one of the characters ABCEGHJ-NPRSTV-Z, then a digit, then the end of the line/string.
Limit: Checks the format only; it cannot tell whether the postal code is assigned.
Examples
Matches
- ✓ K1A 0B1
- ✓ M5V 3L9
- ✓ H2X1Y4
Does not match
- ✗ D1A 0B1
- ✗ K1A 0B
- ✗ 12345
Try it
0 matches (JavaScript engine)
K1A 0B1
M5V 3L9
H2X1Y4
D1A 0B1
K1A 0B
12345Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$/g;
const text = "K1A 0B1";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$')
text = 'K1A 0B1'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$/';
$text = 'K1A 0B1';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$");
String text = "K1A 0B1";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$`)
text := "K1A 0B1"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$");
var text = "K1A 0B1";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$").unwrap();
let text = "K1A 0B1";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z] ?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$/
text = 'K1A 0B1'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are some letters missing?
- Canada Post avoids D, F, I, O, Q and U because they are easily confused with digits or other letters in handwriting and scanning. W and Z are also never used as the first letter.
- What does the first letter represent?
- It identifies a postal district, usually a province or a large part of one, for example K for eastern Ontario or H for Montreal. The pattern accepts all district letters in use.
- How do I accept lowercase input?
- Add the i flag or convert the value with toUpperCase() before testing. Converting is better because you also want to store the code in uppercase.