Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for UK postcode
Regex pattern · validation

Regex for UK postcode

Regex for UK postcode: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i as a UK postcode regex. It matches "SW1A 1AA" and "EC1A1BB", and rejects "12345" and "SW1A". This covers the standard shapes but not every letter rule, and it does not check whether a postcode exists.

Last updated · Maintained by FreeAIToolz · How we test

/^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i
  • SW1A 1AAmatches
  • EC1A1BBmatches
  • M1 1AEmatches
  • B33 8THmatches
  • cr2 6xhmatches
  • 12345no match
  • SW1Ano match
  • SW1A 1Ano match
  • SW1A-1AAno match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
^[A-Z]{1,2}postcode area, 1–2 letters
\d[A-Z\d]?district: a digit, optionally followed by a letter or digit
?optional space
\d[A-Z]{2}$inward code: digit and two letters

Flags: i makes it case-insensitive.

Examples

InputResult
SW1A 1AA✓ Matches
EC1A1BB✓ Matches
M1 1AE✓ Matches
B33 8TH✓ Matches
cr2 6xh✓ Matches
12345✗ No match
SW1A✗ No match
SW1A 1A✗ No match
SW1A-1AA✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

This covers the standard shapes but not every letter rule, and it does not check whether a postcode exists. Uppercase and insert the space before storing.

When to use this pattern

UK address forms and delivery checkers.

Use it in your language

JavaScript

const re = /^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$', re.IGNORECASE)
bool(pattern.search(value))

PHP

$ok = preg_match('~^[A-Z]{1,2}\\d[A-Z\\d]? ?\\d[A-Z]{2}$~i', $value) === 1;

Java

Pattern pattern = Pattern.compile("^[A-Z]{1,2}\\d[A-Z\\d]? ?\\d[A-Z]{2}$", Pattern.CASE_INSENSITIVE);
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`(?i)^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for UK postcode?
/^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i. This covers the standard shapes but not every letter rule, and it does not check whether a postcode exists. Uppercase and insert the space before storing.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abcSW1A 1AAxyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.