Canadian Postal Code Regex Pattern
Validates Canadian postal codes in A1A 1A1 format alternating letters and digits with optional space.
Live Regex Tester
Pattern Breakdown
Code Examples
JavaScript
const regex = /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i; const test = "K1A 0B1"; console.log(regex.test(test)); // true // Extract matches const matches = test.match(regex); console.log(matches);
Python
import re pattern = r'^[A-Z]\d[A-Z]\s?\d[A-Z]\d$' test = "K1A 0B1" match = re.search(pattern, test, re.IGNORECASE) print(match) # Found!
Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]\d[A-Z]\s?\d[A-Z]\d$`)
fmt.Println(re.MatchString("K1A 0B1")) // true
}Common Use Cases
Match Examples
| Input | Result |
|---|---|
| K1A 0B1 | Match |
| 12345 | No Match |
About the Canadian Postal Code Regex
Validates Canadian postal codes in A1A 1A1 format alternating letters and digits with optional space.
Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The canadian postal code pattern is classified as beginner difficulty in the identity & documents category. It works in all major programming languages.
When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.
More Identity & Documents Patterns
Need More Regex Patterns?
Browse our complete library of 100+ regex patterns with interactive testers.
Frequently Asked Questions
What is the Canadian Postal Code regex pattern?
Validates Canadian postal codes in A1A 1A1 format alternating letters and digits with optional space.
How do I use the Canadian Postal Code regex?
Use the pattern /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i in your code. In JavaScript: new RegExp('^[A-Z]\d[A-Z]\s?\d[A-Z]\d$', 'i'). Test it above with your own input.
What does this Canadian Postal Code regex match?
This pattern matches: "K1A 0B1". It does NOT match: "12345". Canadian address validation, shipping, geolocation.
Is the Canadian Postal Code regex beginner-friendly?
This pattern is rated Beginner. It uses basic regex syntax and is easy to understand.
What languages support the Canadian Postal Code regex?
This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.
Can I modify the Canadian Postal Code regex for my use case?
Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.
Related Tools
Social Security Number (US) Regex
Beginner Identity & Documents pattern
US Zip Code Regex
Beginner Identity & Documents pattern
US State Code Regex
Intermediate Identity & Documents pattern
UK Postcode Regex
Intermediate Identity & Documents pattern
Passport Number Regex
Beginner Identity & Documents pattern
IBAN Number Regex
Advanced Identity & Documents pattern