UK postcodes comprise alphanumeric ‘outward’ and ‘inward’ parts separated by a space. For instance,
for the postcode EC4M 8AD,
EC4M is the outward and
8AD is the inward.
Although the space can be inferred if omitted (because the final inward part is always three characters), the encoding is affected by whether or not including a space is treated as the canonical form. Royal Mail says the standard version includes a space and so that is the standard we adopt here.
The anomalous GIR 0AA postcode is included,
despite it officially having been phased out
because, while it no longer appears in the official Postcode Address File (PAF),
it remains in the Postzon product and may be present in older datasets.
Although the Royal Mail guide states that certain letters cannot appear in certain parts of a postcode, the standard naxps below encode all letters on the basis that
- following the guide explicitly would reduce the storage required from 31 bits to 30 bits, i.e. no material difference, and
- there is a history of postcode character exclusions being relaxed when more capacity is required – see e.g. the Netherlands postcode.
Some systems store a UK postcode as exactly eight characters by padding the spaces in the middle,
e.g. B1␣␣␣1AA
(where ␣ represents a single space character)
so this is also included.
As with all postcode systems, a naxp encodes postcodes, but validation is a separate task, in this case typically requiring the PAF.
| Variant | naxp | Notes |
|---|---|---|
default |
\A\A?\9\X? \s!! \9\A\A | GIR \s!! 0AA |
Accepts a postcode with or without a separator space, e.g. B1 1AA or B11AA.Prints with a space, e.g. B1 1AA. |
padding-tolerant |
(\A\A\9\X|\A\A\9\s!?|\A\9\X\s!?|\A\9\s!?\s!?) \s!! \9\A\A | GIR \s!?\s!?\s!! 0AA |
Accepts padding of spaces up to eight characters, e.g. B1 1AA, B1 1AA or B11AA.Prints as standard, i.e. with a single space, e.g. B1 1AA. |
padding-canonical |
(\A\A\9\X|\A\A\9\s!!|\A\9\X\s!!|\A\9\s!!\s!!) \s!! \9\A\A | GIR \s!!\s!!\s!! 0AA |
Accepts padding of spaces up to eight characters, e.g. B1 1AA, B1 1AA or B11AA.Prints the padded version, e.g. B1 1AA. (Useful for writing to IT systems.) |