fanf: (Default)
[personal profile] fanf

A little contribution for anyone else who searches the web for this in the future.

The UK postcode consists of two parts. The first part is the Outward Postcode, or Outcode. This is separated by a single space from the second part which is the Inward Postcode, or Incode. The Outcode directs mail to the correct local area for delivery. The Incode is used to sort the mail at the local area delivery office.

The Outcode has 6 possible formats (as follows) and the Incode is consistently numeric, alpha, alpha format.

  • AN NAA
  • ANN NAA
  • ANA NAA
  • AAN NAA
  • AANN NAA
  • AANA NAA

There are some restrictions on the letters:

  1. The letters [QVX] are not used in the first position.
  2. The letters [IJZ] are not used in the second position.
  3. The only letters to appear in the third position are [ABCDEFGHJKSTUW].
  4. The only letters to appear in the fourth position are [ABEHMNPRVWXY].
  5. The letters [CIKMOV] are not used in the second part.

This translates into a perl extended regex as follows (with slightly relaxed whitespace):

qr{\b
    ([A-PR-UWYZ]\d[\dA-HJKSTUW]? # rules 1,3
    |[A-PR-UWYZ][A-HK-Y]\d[\dABEHMNPRVWXY]? # rules 1,2,4
    )[\t ]{1,2}
    (\d[ABD-HJLNP-UW-Z]{2}) # rule 5
\b}x

Update: more here.

Date: 2008-08-27 18:45 (UTC)
From: [identity profile] knell.livejournal.com
There are a couple of odd exceptions too - you missed out the special postcodes for St Helena and dependencies (STHL 1ZZ, TDCU 1ZZ, etc). Other overseas territories have these too.

Date: 2008-08-27 19:43 (UTC)
From: [identity profile] knell.livejournal.com
.. though it should be noted that not even the Royal Mail's own postcode -> address conversion page doesn't pick these up.

Date: 2008-08-27 19:45 (UTC)
From: [identity profile] knell.livejournal.com
... and that I dropped in a confusing double negative there.

June 2025

S M T W T F S
1234567
8 91011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated 2025-06-10 22:11
Powered by Dreamwidth Studios