🌐
Reddit
reddit.com › r/askuk › what is a valid postcode in uk?
r/AskUK on Reddit: What is a valid postcode in uk?
April 20, 2022 -

I’m trying to get access to a show on bbc player. I’m from Australia and need to be in the UK to watch. So I’ve signed up for a vpn and now I need to register for bbc player but I need to put in a postcode!! Everything online has failed. Please help!!

🌐
UPS
ups.com › media › en › gb › UK_MBE_09302006.pdf pdf
City Postal Code Address Telephone Number Fax Number E-mail Address ABERDEEN
Postal Code · Address · Telephone Number · Fax Number · E-mail Address · ABERDEEN · AB10 6DN · 73 HOLBURN STREET · 01224 211670 · 01224 211671
People also ask

What is the UK postcode format?
UK postcodes have both letters and numbers. They are between five and seven characters long and always start with a letter. They follow the pattern: [A-Z]{1,2}[0-9][A-Z0-9]? [0-9][A-Z]{2}.
🌐
geoapify.com
geoapify.com › postcode-formats-around-the-world
What are the correct postcode formats around the world?
What is the standard postcode format?
The standard postcode format is different from country to country. We recommend checking the postcode format for the country you want to get postcodes for, to make sure your results are accurate.
🌐
geoapify.com
geoapify.com › postcode-formats-around-the-world
What are the correct postcode formats around the world?
Is there an international postcode format?
No. Every country has its own postcode format.
🌐
geoapify.com
geoapify.com › postcode-formats-around-the-world
What are the correct postcode formats around the world?
🌐
Doogal
doogal.co.uk › PostcodeGenerator
Random postcode generator
A free online tool to generate a valid random UK postcode
🌐
Wikipedia
en.wikipedia.org › wiki › Postcodes_in_the_United_Kingdom
Postcodes in the United Kingdom - Wikipedia
19 hours ago - A postcode can be validated against a table of all 1.7 million postcodes in Code-Point Open. The full delivery address including postcode can be validated against the PAF constituting most (but not all) addresses in the UK., by PAF licensed users.
🌐
Royal Mail
royalmail.com › find-a-postcode
Postcode Finder - Find an address │ Royal Mail Group Ltd
Our address and postcode information is held in the Postcode Address File (PAF®). PAF is regulated in the UK by Ofcom.
Find elsewhere
🌐
IdealPostcodes
ideal-postcodes.co.uk › guides › uk-postcode-format
The UK Postcode Format
November 15, 2023 - CSV Download full list of UK postcode units. We publish an open sourced library called Postcode. This package allows you to extract parts of a postcode outlined above. Ideal Postcodes Address Validation is a great solution to ensure your data is accurate and up-to-date.
🌐
GeoPostcodes
geopostcodes.com › home › country hub › uk postcode
UK Postcode - Download Dataset
July 16, 2024 - There is no UK postcode “12345”. United Kingdom postcodes use alphanumeric formats (e.g., AA9 9AA) rather than five-digit ZIP codes; “12345” is a US ZIP format and not used for any town in the UK.
🌐
Geoapify
geoapify.com › postcode-formats-around-the-world
What are the correct postcode formats around the world?
April 16, 2026 - With an API, you can get the postcode from the address string. For example, here is the response that you get for "Rice Ln, Liverpool, L9 1NL, United Kingdom": Discover all the features of Geoapify's Address Lookup, Validation, and Standardization APIs.
🌐
Publishing Service
assets.publishing.service.gov.uk › media › 5a81ebbded915d74e6234d42 › Appendix_C_ILR_2017_to_2018_v1_Published_28April17.pdf pdf
ILR Specification 2017 to 2018 – Appendix C – Valid postcode format
postcodes are an abbreviated form of address, which enable a group of delivery · points (a delivery point being a property or a post box) to be specifically identified. Full valid postcodes can be located at the Royal Mail Postcode Finder website,
🌐
Quora
quora.com › What-is-the-valid-postcode-of-the-UK
What is the valid postcode of the UK? - Quora
Answer (1 of 10): The UK is broken into thousands of Post Codes. When I was I. The British Army I lived in a place called Bordon, Hampshire. The post code used for my address started GU. GU is the sorting code for addresses which come 7ndet the GUILDFORD sorting office. But Guildford is in Surrey...
Top answer
1 of 16
267

I recently posted an answer to this question on UK postcodes for the R language. I discovered that the UK Government's regex pattern is incorrect and fails to properly validate some postcodes. Unfortunately, many of the answers here are based on this incorrect pattern.

I'll outline some of these issues below and provide a revised regular expression that actually works.


Note

My answer (and regular expressions in general):

  • Only validates postcode formats.
  • Does not ensure that a postcode legitimately exists.
    • For this, use an appropriate API! See Ben's answer for more info.

If you don't care about the bad regex and just want to skip to the answer, scroll down to the Answer section.

The Bad Regex

The regular expressions in this section should not be used.

This is the failing regex that the UK government has provided developers (not sure how long this link will be up, but you can see it in their Bulk Data Transfer documentation):

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))[0-9][A-Za-z]{2})$

Problems

Problem 1 - Copy/Paste

See regex in use here.

As many developers likely do, they copy/paste code (especially regular expressions) and paste them expecting them to work. While this is great in theory, it fails in this particular case because copy/pasting from this document actually changes one of the characters (a space) into a newline character as shown below:

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))
[0-9][A-Za-z]{2})$

The first thing most developers will do is just erase the newline without thinking twice. Now the regex won't match postcodes with spaces in them (other than the GIR 0AA postcode).

To fix this issue, the newline character should be replaced with the space character:

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
                                                                                                                                                     ^

Problem 2 - Boundaries

See regex in use here.

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
^^                     ^ ^                                                                                                                                            ^^

The postcode regex improperly anchors the regex. Anyone using this regex to validate postcodes might be surprised if a value like fooA11 1AA gets through. That's because they've anchored the start of the first option and the end of the second option (independently of one another), as pointed out in the regex above.

What this means is that ^ (asserts position at start of the line) only works on the first option ([Gg][Ii][Rr] 0[Aa]{2}), so the second option will validate any strings that end in a postcode (regardless of what comes before).

Similarly, the first option isn't anchored to the end of the line $, so GIR 0AAfoo is also accepted.

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))[0-9][A-Za-z]{2})$

To fix this issue, both options should be wrapped in another group (or non-capturing group) and the anchors placed around that:

^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2}))$
^^                                                                                                                                                                      ^^

Problem 3 - Improper Character Set

See regex in use here.

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
                                                                                       ^^

The regex is missing a - here to indicate a range of characters. As it stands, if a postcode is in the format ANA NAA (where A represents a letter and N represents a number), and it begins with anything other than A or Z, it will fail.

That means it will match A1A 1AA and Z1A 1AA, but not B1A 1AA.

To fix this issue, the character - should be placed between the A and Z in the respective character set:

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
                                                                                        ^

Problem 4 - Wrong Optional Character Set

See regex in use here.

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
                                                                                                                                        ^

I swear they didn't even test this thing before publicizing it on the web. They made the wrong character set optional. They made [0-9] option in the fourth sub-option of option 2 (group 9). This allows the regex to match incorrectly formatted postcodes like AAA 1AA.

To fix this issue, make the next character class optional instead (and subsequently make the set [0-9] match exactly once):

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?)))) [0-9][A-Za-z]{2})$
                                                                                                                                                ^

Problem 5 - Performance

Performance on this regex is extremely poor. First off, they placed the least likely pattern option to match GIR 0AA at the beginning. How many users will likely have this postcode versus any other postcode; probably never? This means every time the regex is used, it must exhaust this option first before proceeding to the next option. To see how performance is impacted check the number of steps the original regex took (35) against the same regex after having flipped the options (22).

The second issue with performance is due to the way the entire regex is structured. There's no point backtracking over each option if one fails. The way the current regex is structured can greatly be simplified. I provide a fix for this in the Answer section.

Problem 6 - Spaces

See regex in use here

This may not be considered a problem, per se, but it does raise concern for most developers. The spaces in the regex are not optional, which means the users inputting their postcodes must place a space in the postcode. This is an easy fix by simply adding ? after the spaces to render them optional. See the Answer section for a fix.


Answer

1. Fixing the UK Government's Regex

Fixing all the issues outlined in the Problems section and simplifying the pattern yields the following, shorter, more concise pattern. We can also remove most of the groups since we're validating the postcode as a whole (not individual parts):

See regex in use here

^([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$

This can further be shortened by removing all of the ranges from one of the cases (upper or lower case) and using a case-insensitive flag. Note: Some languages don't have one, so use the longer one above. Each language implements the case-insensitivity flag differently.

See regex in use here.

^([A-Z][A-HJ-Y]?[0-9][A-Z0-9]? ?[0-9][A-Z]{2}|GIR ?0A{2})$

Shorter again replacing [0-9] with \d (if your regex engine supports it):

See regex in use here.

^([A-Z][A-HJ-Y]?\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$

2. Simplified Patterns

Without ensuring specific alphabetic characters, the following can be used (keep in mind the simplifications from 1. Fixing the UK Government's Regex have also been applied here):

See regex in use here.

^([A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}|GIR ?0A{2})$

And even further if you don't care about the special case GIR 0AA:

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

3. Complicated Patterns

I would not suggest over-verification of a postcode as new Areas, Districts and Sub-districts may appear at any point in time. What I will suggest potentially doing, is added support for edge-cases. Some special cases exist and are outlined in this Wikipedia article.

Here are complex regexes that include the subsections of 3. (3.1, 3.2, 3.3).

In relation to the patterns in 1. Fixing the UK Government's Regex:

See regex in use here

^(([A-Z][A-HJ-Y]?\d[A-Z\d]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?\d[A-Z]{2}|BFPO ?\d{1,4}|(KY\d|MSR|VG|AI)[ -]?\d{4}|[A-Z]{2} ?\d{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$

And in relation to 2. Simplified Patterns:

See regex in use here

^(([A-Z]{1,2}\d[A-Z\d]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?\d[A-Z]{2}|BFPO ?\d{1,4}|(KY\d|MSR|VG|AI)[ -]?\d{4}|[A-Z]{2} ?\d{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$

3.1 British Overseas Territories

The Wikipedia article currently states (some formats slightly simplified):

  • AI-1111: Anguila
  • ASCN 1ZZ: Ascension Island
  • STHL 1ZZ: Saint Helena
  • TDCU 1ZZ: Tristan da Cunha
  • BBND 1ZZ: British Indian Ocean Territory
  • BIQQ 1ZZ: British Antarctic Territory
  • FIQQ 1ZZ: Falkland Islands
  • GX11 1ZZ: Gibraltar
  • PCRN 1ZZ: Pitcairn Islands
  • SIQQ 1ZZ: South Georgia and the South Sandwich Islands
  • TKCA 1ZZ: Turks and Caicos Islands
  • BFPO 11: Akrotiri and Dhekelia
  • ZZ 11 & GE CX: Bermuda (according to this document)
  • KY1-1111: Cayman Islands (according to this document)
  • VG1111: British Virgin Islands (according to this document)
  • MSR 1111: Montserrat (according to this document)

An all-encompassing regex to match only the British Overseas Territories might look like this:

See regex in use here.

^((ASCN|STHL|TDCU|BBND|[BFS]IQQ|GX\d{2}|PCRN|TKCA) ?\d[A-Z]{2}|(KY\d|MSR|VG|AI)[ -]?\d{4}|(BFPO|[A-Z]{2}) ?\d{2}|GE ?CX)$

3.2 British Forces Post Office

Although they've been recently changed it to better align with the British postcode system to BF# (where # represents a number), they're considered optional alternative postcodes. These postcodes follow(ed) the format of BFPO, followed by 1-4 digits:

See regex in use here

^BFPO ?\d{1,4}$

3.3 Santa?

There's another special case with Santa (as mentioned in other answers): SAN TA1 is a valid postcode. A regex for this is very simply:

^SAN ?TA1$
2 of 16
263

I'd recommend taking a look at the UK Government Data Standard for postcodes [link now dead; archive of XML, see Wikipedia for discussion]. There is a brief description about the data and the attached xml schema provides a regular expression. It may not be exactly what you want but would be a good starting point. The RegEx differs from the XML slightly, as a P character in third position in format A9A 9AA is allowed by the definition given.

The RegEx supplied by the UK Government was:

([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})

As pointed out on the Wikipedia discussion, this will allow some non-real postcodes (e.g. those starting AA, ZY) and they do provide a more rigorous test that you could try.

🌐
GeoNames
geonames.org › postalcode-search.html
Postal Codes United Kingdom
GeoNames Home • Postal Codes • Download / Webservice • Forum • Blog • Sitemap
🌐
Andy Pearce
andy-pearce.com › blog › posts › 2015 › Aug › validating-uk-postcodes
Validating UK Postcodes
August 18, 2015 - I find myself needing to enter UK addresses on a fairly regular basis and it never fails to amaze me how poor some of the syntax checking is - basic validation of a UK postcode is really not e...
🌐
Your Virtual Office London
yourvirtualofficelondon.co.uk › blog › zip-code-uk
UK Postcode vs the US Zip Code | YVOL
This online service allows you ... A UK postal code consists of 5 to 7 alphanumeric characters, divided into two parts: the outward code and the inward code, separated by a space....
🌐
Free Map Tools
freemaptools.com › download-uk-postcode-lat-lng.htm
Download UK Postcodes with Latitude and Longitude
Download the centroid coordinates of each UK Postcode. Only active (valid) postcodes are included.
🌐
Crystal Roof
crystalroof.co.uk › postcode-districts
All postcodes in Greater London - Crystal Roof
Complete list of postcodes in Greater London. Map of Greater London postcode areas and postcode districts.
🌐
American Express
americanexpress.com › content › dam › amex › uk › merchant › PaulSmith_Participating_Locations.pdf pdf
Name Address City Postcode Floral Street
Postcode · Floral Street · 40-44 Floral Street Covent Garden London WC2E 9TB · London · WC2E 9TB · Westbourne House · 122 Kensington Park Road Notting Hill London W11 2EP · London · W11 2EP · Heathrow T3 · Unit RU3108 Terminal Three, Heathrow Airport London TW6 1QG ·
🌐
UK Postcode
ukpostcode.co.uk › random.htm
List Random UK Postcodes
This page will initially return 10 random postcodes. This can be used for development purposes or for using a random sample.
🌐
UK Postcode
ukpostcode.co.uk › validate-postcode.htm
Validate A UK Postcode - UK Postcode
Geoinformation, maps, downloads and statistics for UK Postcodes · Use this page to validate a UK postcode against a list of known existing postcodes. Type the postcode below and click Validate.
🌐
Cabinetoffice
supplierregistration.cabinetoffice.gov.uk › faq › en_GB › UK_postcode
I am not based in the UK and the form is requesting a UK postcode, how do I proceed? - Supplier Registration Service
If you are not based in the UK, you can use the Royal Mail postcode EC1A 1AA as a generic postcode and the 8 digit number "00000000" as a dummy number for the UK Companies House Registration number if required.