Regex for Canadian Postal Code — Pattern Explained with Examples
DodaTech
Updated 2026-06-20
2 min read
In this tutorial, you'll learn about Regex for Canadian Postal Code. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Canadian postal codes are six-character alphanumeric codes in an alternating letter-digit pattern separated by a space. This pattern validates the standard A#A #A# format used by Canada Post for mail sorting and delivery.
The Pattern
/^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i
Pattern Breakdown
| Part | Meaning |
|---|---|
^ |
Start-of-string anchor |
[A-Z]\d[A-Z] |
Letter-digit-letter (FSA — forward sortation area) |
\s? |
Optional whitespace separator |
\d[A-Z]\d |
Digit-letter-digit (LDU — local delivery unit) |
$ |
End-of-string anchor |
i |
Case-insensitive flag |
Matches
K1A 0B1H0H 0H0V6B 4Y8M5V 2T6T2P 1J9
Does NOT Match
K1A0B1(no space)12345K1A 0BABCDA1A1A1A1K1A-0B1
Language Examples
JavaScript
const caPostalRegex = /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i;
console.log(caPostalRegex.test('K1A 0B1')); // true
console.log(caPostalRegex.test('12345')); // false
Python
import re
pattern = r'^[A-Z]\d[A-Z]\s?\d[A-Z]\d$'
print(bool(re.match(pattern, 'K1A 0B1', re.IGNORECASE))) # True
print(bool(re.match(pattern, '12345', re.IGNORECASE))) # False
Common Pitfalls
- The first letter denotes a province or region (
Kfor Ontario,Vfor British Columbia, etc.) — some letters likeD,F,I,O,Q, andUare not used in the first position by Canada Post - The space is technically optional but strongly recommended — Canada Post formatting standards include it
- Input should be reformatted to uppercase for consistency, even though the pattern is case-insensitive
- The letters
D,F,I,O,Q, andUdo not appear in Canadian postal codes at all due to optical character recognition issues - A valid format does not guarantee a real postal code — the code must exist in the Canada Post database for delivery
Real-World Use Cases
- Canadian e-commerce platforms — validate shipping addresses for orders within Canada
- Address autocomplete services — validate partial postal code input before suggesting addresses
- Mailing list hygiene — clean and standardize postal codes in customer databases before bulk mailings
FAQ
Related Patterns
Regex for UK Postcode Regex for ZIP/Postal Code
← Previous
Regex for UK Postcode — Pattern Explained with Examples
Next →
Regex for Semantic Version (SemVer) — Pattern Explained with Examples
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro