RegEx for matching UK Postcodes

前端 未结 30 2296
广开言路
广开言路 2020-11-22 01:38

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 in

30条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 02:00

    There is no such thing as a comprehensive UK postcode regular expression that is capable of validating a postcode. You can check that a postcode is in the correct format using a regular expression; not that it actually exists.

    Postcodes are arbitrarily complex and constantly changing. For instance, the outcode W1 does not, and may never, have every number between 1 and 99, for every postcode area.

    You can't expect what is there currently to be true forever. As an example, in 1990, the Post Office decided that Aberdeen was getting a bit crowded. They added a 0 to the end of AB1-5 making it AB10-50 and then created a number of postcodes in between these.

    Whenever a new street is build a new postcode is created. It's part of the process for obtaining permission to build; local authorities are obliged to keep this updated with the Post Office (not that they all do).

    Furthermore, as noted by a number of other users, there's the special postcodes such as Girobank, GIR 0AA, and the one for letters to Santa, SAN TA1 - you probably don't want to post anything there but it doesn't appear to be covered by any other answer.

    Then, there's the BFPO postcodes, which are now changing to a more standard format. Both formats are going to be valid. Lastly, there's the overseas territories source Wikipedia.

    +----------+----------------------------------------------+
    | Postcode |                   Location                   |
    +----------+----------------------------------------------+
    | AI-2640  | Anguilla                                     |
    | 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 1AA | Gibraltar                                    |
    | PCRN 1ZZ | Pitcairn Islands                             |
    | SIQQ 1ZZ | South Georgia and the South Sandwich Islands |
    | TKCA 1ZZ | Turks and Caicos Islands                     |
    +----------+----------------------------------------------+

    Next, you have to take into account that the UK "exported" its postcode system to many places in the world. Anything that validates a "UK" postcode will also validate the postcodes of a number of other countries.

    If you want to validate a UK postcode the safest way to do it is to use a look-up of current postcodes. There are a number of options:

    • Ordnance Survey releases Code-Point Open under an open data licence. It'll be very slightly behind the times but it's free. This will (probably - I can't remember) not include Northern Irish data as the Ordnance Survey has no remit there. Mapping in Northern Ireland is conducted by the Ordnance Survey of Northern Ireland and they have their, separate, paid-for, Pointer product. You could use this and append the few that aren't covered fairly easily.

    • Royal Mail releases the Postcode Address File (PAF), this includes BFPO which I'm not sure Code-Point Open does. It's updated regularly but costs money (and they can be downright mean about it sometimes). PAF includes the full address rather than just postcodes and comes with its own Programmers Guide. The Open Data User Group (ODUG) is currently lobbying to have PAF released for free, here's a description of their position.

    • Lastly, there's AddressBase. This is a collaboration between Ordnance Survey, Local Authorities, Royal Mail and a matching company to create a definitive directory of all information about all UK addresses (they've been fairly successful as well). It's paid-for but if you're working with a Local Authority, government department, or government service it's free for them to use. There's a lot more information than just postcodes included.

提交回复
热议问题