UK Postcode Regex

Most UK postcode regexes seem to be strict validators. For London Cinema I am more interested in what the user meant than I am in making sure they have entered an exactly legal postcode.

The Google geocoding stuff that I use in my geocoder is surprisingly unliberal in what it accepts (for example it won’t accept a postcode with a missing space), probably because it is using a strict validator like those you can find all over the web. I wrote this disambiguation function that tries to turn possibly-funky user input into a canonical postcode. The test uses genuine inputs to London Cinema from the last few days.

function disambiguate_uk_postcode($s) {
    $target = str_replace(' ', '', mb_strtoupper($s));
    $postcode_finder = '/^([A-Z][A-Z]?)([O0-9][O0-9]?)([A-Z]?)([O0-9])([A-Z][A-Z])$/';
    if (preg_match($postcode_finder, $target, $matches)) {
        return $matches[1] . str_replace('O', '0', $matches[2]) . $matches[3]
            . " " . str_replace('O', '0', $matches[4]) . $matches[5];
    } else {
        return $s;
    }
}

function test_disambiguate_uk_postcode() {
    assert(disambiguate_uk_postcode('se 15 5 ed') === 'SE15 5ED');
    assert(disambiguate_uk_postcode('se229ef') === 'SE22 9EF');
    assert(disambiguate_uk_postcode('wc1n1as') === 'WC1N 1AS');
    assert(disambiguate_uk_postcode('w111pg') === 'W11 1PG');
    assert(disambiguate_uk_postcode('e113bz') === 'E11 3BZ');
    assert(disambiguate_uk_postcode('cro 5al') === 'CR0 5AL');
    assert(disambiguate_uk_postcode('E15JA') === 'E1 5JA');
    assert(disambiguate_uk_postcode('ha80hb') === 'HA8 0HB');
    assert(disambiguate_uk_postcode('E4 7 DT') === 'E4 7DT');
    assert(disambiguate_uk_postcode('SW179HN') === 'SW17 9HN');
    assert(disambiguate_uk_postcode('south woodford') === 'south woodford');
}

/*
Copyright (c) 2009 Thomas David Baker

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.