BytePane

UK Postcode Regex Pattern

Validates UK postcodes in various formats (A1 1AA, A11 1AA, AA1 1AA, etc.) with optional space.

🪪
Identity & Documents
Intermediate
Difficulty
Universal
Language
i
Flags
// Regular Expression
/^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i

Live Regex Tester

Pattern Breakdown

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

Code Examples

JavaScript

const regex = /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i;
const test = "SW1A 1AA";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$'
test = "SW1A 1AA"
match = re.search(pattern, test, re.IGNORECASE)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$`)
    fmt.Println(re.MatchString("SW1A 1AA")) // true
}

Common Use Cases

UK address validationshippinglocation services

Match Examples

InputResult
SW1A 1AAMatch
12345No Match

About the UK Postcode Regex

Validates UK postcodes in various formats (A1 1AA, A11 1AA, AA1 1AA, etc.) with optional space.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The uk postcode pattern is classified as intermediate 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 UK Postcode regex pattern?

Validates UK postcodes in various formats (A1 1AA, A11 1AA, AA1 1AA, etc.) with optional space.

How do I use the UK Postcode regex?

Use the pattern /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i in your code. In JavaScript: new RegExp('^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$', 'i'). Test it above with your own input.

What does this UK Postcode regex match?

This pattern matches: "SW1A 1AA". It does NOT match: "12345". UK address validation, shipping, location services.

Is the UK Postcode regex beginner-friendly?

This pattern is rated Intermediate. It uses some advanced features like character classes and quantifiers.

What languages support the UK Postcode 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 UK Postcode 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