Skip to content

Regex for UK Postal Code

This regex matches UK postcodes, which follow several alphanumeric formats such as A1 1AA, A11 1AA, AA1 1AA, AA11 1AA, A1A 1AA, and AA1A 1AA. The outward code (before the space) identifies the postal district, and the inward code (after the space) identifies the specific delivery point. The space between the two parts is optional in this pattern.

Pattern flags: i
^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$
Test this pattern in the Regex Tester →

What is the regex pattern for UK Postal Code?

The regex pattern for UK Postal Code is ^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$ with the i flag. This regex matches UK postcodes, which follow several alphanumeric formats such as A1 1AA, A11 1AA, AA1 1AA, AA11 1AA, A1A 1AA, and AA1A 1AA. The outward code (before the space) identifies the postal district, and the inward code (after the space) identifies the specific delivery point. The space between the two parts is optional in this pattern. This pattern is commonly used for uk address validation and delivery zone calculation.

Test Examples

Match
SW1A 1AA
Matches: SW1A 1AA
Match
EC1A 1BB
Matches: EC1A 1BB
Match
M1 1AE
Matches: M1 1AE

Common Uses

Variations

Strict with space

^[A-Z]{1,2}\d[A-Z\d]? \d[A-Z]{2}$

Requires the space between parts

London only

^(EC|WC|E|N|NW|SE|SW|W)\d[A-Z\d]?\s?\d[A-Z]{2}$

Only London postal districts

Outward code only

^[A-Z]{1,2}\d[A-Z\d]?$

Just the first part (postal district)

Frequently Asked Questions

What does a UK postcode look like?

UK postcodes have two parts: an outward code (1-4 characters identifying the postal district) and an inward code (always 1 digit followed by 2 letters). Examples include SW1A 1AA, M1 1AE, and B33 8TH.

Is the space required in a UK postcode?

Officially, yes. Royal Mail uses a single space between the outward and inward codes. However, postcodes are often written or stored without the space, so most validation patterns make it optional.

Are UK postcodes case-sensitive?

No, UK postcodes are case-insensitive. They are conventionally written in uppercase, but lowercase is equally valid and should be accepted in forms.

Related Patterns

US ZIP Code

^\d{5}(-\d{4})?$

Canadian Postal Code

^[A-CEGHJ-NPRSTVXY]\d[A-CEGHJ-NPRSTV-Z]\...

Related Reading

Regex Cheat Sheet with Examples for Developers → URL Encoding Special Characters →