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.

Advertisements
UK Postcode Validation Regex

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 )

w

Connecting to %s