Contact
UK Postcode Regex
A UK postcode has an outward code of two to four characters and an inward code of one digit and two letters. Typical examples are SW1A 1AA, M1 1AE and EC1A 1BB. This pattern accepts all the standard outward shapes and makes the separating space optional, which covers how people normally type postcodes into delivery and address forms. Input should be uppercase, so convert it first. The honest limit is that the pattern does not apply the detailed letter restrictions for each position, so some impossible postcodes still match.
Pattern (JavaScript)
/^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/Starts at the start of the line/string, then an uppercase letter, between 1 and 2 times, then a digit, then one of the characters A-Z0-9, optionally (zero or one time), then a space, optionally (zero or one time), then a digit, then an uppercase letter, exactly 2 times, then the end of the line/string.
Limit: Checks the format only; it cannot tell whether the postcode exists.
Examples
Matches
- ✓ SW1A 1AA
- ✓ M1 1AE
- ✓ EC1A1BB
- ✓ B33 8TH
Does not match
- ✗ SW1A 1A
- ✗ 12345
- ✗ SW1A-1AA
Try it
0 matches (JavaScript engine)
SW1A 1AA
M1 1AE
EC1A1BB
B33 8TH
SW1A 1A
12345
SW1A-1AACode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/g;
const text = "SW1A 1AA";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$')
text = 'SW1A 1AA'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/';
$text = 'SW1A 1AA';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$");
String text = "SW1A 1AA";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$`)
text := "SW1A 1AA"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$");
var text = "SW1A 1AA";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$").unwrap();
let text = "SW1A 1AA";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/
text = 'SW1A 1AA'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is the space optional?
- Many users type postcodes without the space, especially on mobile keyboards. Accept both, then insert the space before the last three characters when you store the value.
- Does this accept the special GIR 0AA postcode?
- No. GIR 0AA starts with three letters, so it falls outside the normal outward code shape. Add |GIR ?0AA as an extra alternative inside the anchors if you need that historic postcode.
- How can I make the check stricter?
- The official rules exclude certain letters in certain positions, for example Q, V and X in the first position. You can encode those in character classes, but an address lookup service gives better results.