UK Postcode Validation Regex

So, the Government offer us a regex to validate UK postcodes. According to Wikipedia it should be:

^([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})$

Nice. That’s not complicated, is it? Let’s ignore the case and make that simpler:

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

So what’s the GIR crud? Apparently it was for Girobank, and now used by Santander. Thanks for that. It’s obsolete, so let’s ditch that (Santander – use a new postcode!)

That leaves:

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

This breaks down as:

One letter followed by either one or two numbers
OR
One letter followed by a second letter that must be one of ABCDEFGHJ
KLMNOPQRSTUVWXY(i.e..not I or Z) and then followed by either one or two
numbers
OR
One letter followed by one number and then another letter
OR
A two part post code where the first part must be:
a) One letter followed by a second letter that must be one of ABCDEFGHJKLMNOPQRSTUVWXY(i.e..not I or Z) and then followed by one number and
optionally a further letter after that
AND
b) The second part (separated by a space from the first part) must be One number followed by two letters.

Note the [A-HJ-Y] groups – someone sensibly decided to exclude ‘i’ from postcodes (presumably to avoid confusion with 1) – but only some sections.  For example ‘IP’ is Ipswich. Shame about the consistency, chaps.

That seems to work nicely, but it’s still a bit gnarly. If one were looking up a postcode to validate it (and perform geo-lookup) we could simplify it further. Pretty good coverage would be given by:

^[A-Z]{1,2}[0-9]{1,2}[A-Z]?(\\s*[0-9][A-Z]{1,2})?$

Now, this would allow invalid Postcodes – but if you’re performing a lookup (Codepoint Open anyone?), you’ll find that it’s invalid then. This regex will restrict input to the right ball-park. I’d recommend this, or the slightly more complicated option above. Forget Girobank.

UK Postcode Validation Regex

3 thoughts on “UK Postcode Validation Regex

  1. ianjpage says:

    Unfortunately as written, that regex allows something like the following as a valid postcode format:

    AA99\sssss1AA

    Assuming that the first backslash is to escape the second (for example in a Java string), removing it still allows incorrectly formatted postcodes, e.g.:

    AA99 (missing inward code)
    AA99A 9AA (letter at end of outward ode is only permitted after a single digit)
    AA99 9AA (although your particular implementation might allow this)

    I suggest the following regex instead:

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

  2. Duncan says:

    Really neat simplification, thanks. I tweaked it a little bit by adding a ? after the space to cater for those ejits (like me) who don’t always put the space between the two parts of the postcode. (I know, but it’s OK for my need). I used….
    ^(([A-Z][0-9]{1,2})|(([A-Z][A-HJ-Y][0-9]{1,2})|(([A-Z][0-9][A-Z])|([A-Z][A-HJ-Y][0-9]?[A-Z])))) ?[0-9][A-Z]{2}$

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.