I'm after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For instance:
Matches
- CW3 9SS
- SE5 0EG
- SE50EG
- se5 0eg
- WC2H 7LT
No Match
- aWC2H 7LT
- WC2H 7LTa
- WC2H
Are there any official or even semi-official regexes in use for this kind of thing? Any other advice as to formatting and storing these in a database?
According to this Wikipedia table
This pattern cover all the cases
When using it on Android\Java use \\d
Have a look at the python code on this page:
http://www.brunningonline.net/simon/blog/archives/001292.html
I've used it to process postcodes for me.
Here's a regex based on the format specified in the documents which are linked to marcj's answer:
The only difference between that and the specs is that the last 2 characters cannot be in [CIKMOV] according to the specs.
Edit: Here's another version which does test for the trailing character limitations.
Some of the regexs above are a little restrictive. Note the genuine postcode: "W1K 7AA" would fail given the rule "Position 3 - AEHMNPRTVXY only used" above as "K" would be disallowed.
the regex:
Seems a little more accurate, see the Wikipedia article entitled 'Postcodes in the United Kingdom'.
Note that this regex requires uppercase only characters.
The bigger question is whether you are restricting user input to allow only postcodes that actually exist or whether you are simply trying to stop users entering complete rubbish into the form fields. Correctly matching every possible postcode, and future proofing it, is a harder puzzle, and probably not worth it unless you are HMRC.
Basic rules:
Postal codes in the U.K. (or postcodes, as they’re called) are composed of five to seven alphanumeric characters separated by a space. The rules covering which characters can appear at particular positions are rather complicated and fraught with exceptions. The regular expression just shown therefore sticks to the basic rules.
Complete rules:
If you need a regex that ticks all the boxes for the postcode rules at the expense of readability, here you go:
Source: https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s16.html
Tested against our customers database and seems perfectly accurate.
We were given a spec:
We came up with this:
But note - this allows any number of spaces in between groups.