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.
^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$ 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
SW1A 1AA SW1A 1AA EC1A 1BB EC1A 1BB M1 1AE M1 1AE Common Uses
- ✓ UK address validation
- ✓ Delivery zone calculation
- ✓ Store locator
- ✓ Geographic data cleaning
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.