BytePane

Canadian Postal Code Regex Pattern

Validates Canadian postal codes in A1A 1A1 format alternating letters and digits with optional space.

🪪
Identity & Documents
Beginner
Difficulty
Universal
Language
i
Flags
// Regular Expression
/^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i

Live Regex Tester

Pattern Breakdown

^[A-Z]\d[A-Z]\s?\d[A-Z]\d$
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

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

Canadian address validationshippinggeolocation

Match Examples

InputResult
K1A 0B1Match
12345No 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.

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