How to match UK zip codes with a prefix?

I have several restaurants that all deliver to certain zip codes in London, for example:

  • EC1
  • WC1
  • WC2
  • W1

When someone searches for a restaurant that takes them home, he enters the full zip code.

Some people correctly enter a zip code with space, some of them simply enter all letters and numbers attached without a space separator. To reconcile things, I delete any space in the zip code before trying to match.

So far, I've been matching the zip code with prefixes, just checking to see if it starts with a given prefix, but then I realized that this is not reliable:

  • WC1E123 => correct match for WC1
  • W1ABC => correct match for W1
  • W10ABC=> incorrect match for W1, must match only the prefixW10

, , , W1/W10

- , ?

+3
6

6 :

  • A9 9AA
  • A9A 9AA
  • A99 9AA
  • AA9 9AA
  • AA9A 9AA
  • AA99 9AA

, . - ; - .

Validation

, , , , , , , !

, , , ( http://www.qas.co.uk/knowledge-centre/product-information/address-postcode-finder.htm ( )) , , / - , , .

, - W1ABC W10ABC - , .

, , , (outcode) - . (incode) 9AA, digit-alpha-alpha, , , out, W1 From W1 0AA, W10 W10 0AA.

, , .

+15

, , lookahead, , ( )

(GIR|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]))(?=( )?[0-9][ABD-HJLNP-UW-Z]{2})

, (, AA ..), , .

. , , , , . :

(GIR|([A-Z-[QVX][0-9][0-9]?)|(([A-Z-[QVX][A-Z-[IJZ][0-9][0-9]?)|(([A-Z-[QVX][0-9][A-HJKSTUW])|([A-Z-[QVX][A-Z-[IJZ][0-9][ABEHMNPRVWXY]))))(?=( )?[0-9][A-Z-[CIKMOV]{2})
+1

php

$first=trim(substr(trim($postcode),0,-3));

. . , ( 2 ) , 3 . , . : - , .

,

, , , http://postcodes.io/.

http://api.postcodes.io/postcodes/W11%202AQ JSON .

{
    "status": 200,
    "result": {
        "postcode": "W11 2AQ",
        "quality": 1,
        "eastings": 524990,
        "northings": 181250,
        "country": "England",
        "nhs_ha": "London",
        "longitude": -0.200056238526337,
        "latitude": 51.5163540527233,
        "parliamentary_constituency": "Kensington",
        "european_electoral_region": "London",
        "primary_care_trust": "Kensington and Chelsea",
        "region": "London",
        "lsoa": "Kensington and Chelsea 004A",
        "msoa": "Kensington and Chelsea 004",
        "nuts": "Colville",
        "incode": "2AQ",
        "outcode": "W11",
        "admin_district": "Kensington and Chelsea",
        "parish": "Kensington and Chelsea, unparished area",
        "admin_county": null,
        "admin_ward": "Colville",
        "ccg": "NHS West London (Kensington and Chelsea, Queenís Park and Paddington)",
        "codes": {
            "admin_district": "E09000020",
            "admin_county": "E99999999",
            "admin_ward": "E05009392",
            "parish": "E43000210",
            "ccg": "E38000202"
        }
    }
}

JSON - "outcode": "W11", , , , .

"": 524990, "northings": 181250, . - . .

+1

:

UK Postal Codes Format

(: https://www.getthedata.com/postcode) ( )

, , Outcode, ( ) - .

PHP :

$outcode = substr($postcode_no_space, 0, -3)

, , , , .

+1

Since you can calculate the length of the zip code that was entered by the client, and in the formats for zip codes there is always 9AA at the end, you can split the code into several cases and return the matches by doing the following

firstPart -> postcode with last 3 characters removed
firstPartLength -> length of firstPart
switch (firstPartLength){
    case 2:
        code to compare prefix against A99AA format
    case 3:
        code to compare prefix against A9A9AA, A999AA, AA99AA format
    case 4:
        code to compare prefix against AA999AA format

or if you don’t want to truncate the last 3 characters,

length -> length of postcode
switch (length){
    case 5:
        code to compare prefix against A99AA format
    case 6:
        code to compare prefix against A9A9AA, A999AA, AA99AA format
    case 7:
        code to compare prefix against AA999AA format
0
source

Given the assumption that each zip code ends in 9AA and each input zip code is valid, the following regular expression can be used to match the area prefix:

^(\w{2,4})\s*[0-9][a-zA-Z]{2}$

The first capture group returns the desired prefix.

0
source

All Articles