UK Postcode
Match UK postcodes in the common outward + inward format (e.g. SW1A 1AA, EC1V 9LB).
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("[A-Z]{1,2}\\d[A-Z\\d]? ?\\d[A-Z]{2}", "gi");
const input = "SW1A 1AA";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}", re.IGNORECASE)
input_text = "SW1A 1AA"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}`)
input := `SW1A 1AA`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2} (flags: gi)Raw source: [A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}
How it works
Examples
Input
SW1A 1AAMatches
SW1A 1AA
Input
EC1V 9LBMatches
EC1V 9LB
Input
B33 8THMatches
B33 8TH
Common use cases
- •UK address validation
- •Geocoding integration
- •Delivery route planning
- •Audience targeting by region
Related patterns
Canadian Postal Code
ValidationMatch Canadian postal codes in the A1A 1A1 or A1A1A1 format with valid first-letter prefixes.
Email Address Validation
ValidationMatch and validate email addresses in the standard user@domain.tld format.
International Phone (E.164)
ValidationValidate phone numbers in ITU-T E.164 international format: a + sign followed by 2–15 digits, first digit non-zero.
US Phone Number
ValidationMatch US phone numbers in common formats: (555) 867-5309, 555-867-5309, 5558675309.