Things to do with postcodes

Enter a UK postcode to get deeplinks into databases and applications which return data or services based on your chosen postcode.

Try an example: SW1A 1AA

Or use the postcode drilldown below.

Programmers guide to UK postcodes

How do we identify a UK postcode? Read on for everything you need to know about programming with postcodes.

Postcode format

Each postcode consists of between two and four characters, followed by a space, followed by another three characters.

The first set of characters are the outcode (sometimes known as the outward code) whilst the second set are the incode (sometimes known as the inward code).

The outcode is itself composed of two sections, and the incode is itself also composed of two sections, which breakdown as follows:

Syntax described

Postcode Area
Starts the outcode. Consists of one or two letters.
Postcode District
Adding one or two characters to postcode area completes the outcode and makes a postcode district. The first character is always numeric. The optional second character may be a letter or a number.
A [space] character is used to separate the outcode from the incode.
Postcode Sector
Adding a single number after the space creates a postcode sector. This number is the start of the incode.
Unit Postcode
Adding a further two letters creates a unit postcode, completing both the incode and the postcode itself.

Geographical areas

Each part of the postcode defines a geographical area.

The map below shows the areas defined by the postcode area, postcode district, postcode sector and unit postcode for example postcode WC2B 4AB:

Parsing a UK postcode

The approach for parsing a postcode varies in complexity depending on two things:

  1. Which part(s) of the postcode you need.
  2. What format you are expecting the postcode in.

If you just need outcode and incode, and you are expecting a postcode in the standard format - i.e. outcode and incode separated by a single space character - you can simply split on [space].

Example PHP code below:

array($outcode, $incode) = explode(' ', $postcode);

If you are familiar with other languages, and would like to contribute a method in another language, we would love to hear from you on Twitter or by email.

If you need postcode area, postcode district, postcode sector and unit postcode you can use a regular expression.

In the example below, we also allow for formatting variations, where incode and outcode may be separated by zero, one, or multiple [space] characters:

/^(((([A-Z][A-Z]{0,1})[0-9][A-Z0-9]{0,1}) {0,}[0-9])[A-Z]{2})$/

➜ See a demo on regex101.

This matches, in order:

  1. Unit Postcode
  2. Postcode Sector
  3. Postcode District
  4. Postcode Area

You may find it more convenient to match elements individually, in which case you just need to rearrange the parenthesis.

This example matches all possible elements, and also includes the i modifier to allow for upper and lower case letters:

/^(([A-Z][A-Z]{0,1})([0-9][A-Z0-9]{0,1})) {0,}(([0-9])([A-Z]{2}))$/i

➜ See a demo on regex101.

This matches, in order:

  1. Outcode
  2. First half of outcode (postcode area)
  3. Second half of outcode
  4. Incode
  5. First half of incode
  6. Second half of incode

Validating a UK postcode

You can use one of the parsing regular expressions above to validate that a string meets the correct format for a UK postcode.

However, this does not mean that the string matches an actual postcode!

The only safe check you can do here, is to look the postcode up in a current postcode database.

The open data database is the Ordnance Survey's Code-Point Open. This is sufficient for most purposes.

More comprehensive and regularly updated is the Royal Mail's Postcode Address File (PAF). However PAF is not open, and is subject to a license fee.

Sources and further reading

Postcode drilldown