UK Postcode Regex Pattern
Validates UK postcodes in various formats (A1 1AA, A11 1AA, AA1 1AA, etc.) with optional space.
Live Regex Tester
Pattern Breakdown
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
Match Examples
| Input | Result |
|---|---|
| SW1A 1AA | Match |
| 12345 | No 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.
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 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
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
Canadian Postal Code Regex
Beginner Identity & Documents pattern
Passport Number Regex
Beginner Identity & Documents pattern
IBAN Number Regex
Advanced Identity & Documents pattern