Regex for UK postcode
UK postcode in either compact or spaced form.
The UK postcode regex is ^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$ — copy it below, or open it in the explainer for a token-by-token breakdown.
The pattern
^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$
What it matches
SW1A 1AAM1 1AEB33 8THCR0 6XHEC1A 1BB
What it doesn't match
12345SW1ASW1A 1ASW1A1AAA
Notes & gotchas
UK postcodes have an area+district half and a sector+unit half, optionally separated by a space. Format varies (A1 1AA, A1A 1AA, AA1 1AA, AA1A 1AA, AA11 1AA, etc.) but this pattern covers all common forms.
Code in your language
Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.
Open in explainer →Token-by-token breakdown
Every part of the pattern, left to right:
| Token | Meaning |
|---|---|
^ | start of string (or line in multiline mode) |
[A-Z]{1,2} | between 1 and 2 times: any of: uppercase letters |
\d | any digit (0–9) |
[A-Z\d]? | optional (zero or one): any of: uppercase letters, digits (0–9) |
\s? | optional (zero or one): whitespace |
\d | any digit (0–9) |
[A-Z]{2} | exactly 2 times: any of: uppercase letters |
$ | end of string (or line in multiline mode) |
About this pattern
UK identifier formats are set by HMRC, Royal Mail, and the BACS payments scheme. The regex here matches the format; full validation may need an additional checksum or lookup.
Quick usage in different languages
This exact pattern — with the correct escaping and idioms for each language:
- JavaScript:
/^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/.test(value) - Python:
re.match(r"^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$", value) - Java:
Pattern.compile("^[A-Z]{1,2}\\d[A-Z\\d]?\\s?\\d[A-Z]{2}$").matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, @"^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$") - Go:
regexp.MustCompile(`^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$`).MatchString(value) - Ruby:
/^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/.match?(value) - PHP:
preg_match('~^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$~', $value)
The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.
Common pitfalls
- Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
- Uppercase only. Ranges like [A-Z] won't match lowercase. Add a-z (or the i flag) if lowercase input should be accepted.
- Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
- Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the uk postcode against a source of truth (database, API, or checksum) where it matters.
Standards & sources
This pattern is based on the following authoritative specification(s) and issuing authorities. Formats can change — always confirm against the primary source.
Related patterns
More patterns in the United Kingdom 🇬🇧 category:
See also
Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.
Want more patterns? Browse the full library →