Postcodes in the United Kingdom
UK postcodes are generally geographical, meaning they pinpoint a location or define an area.
For this reason they are commonly used to geocode datasets or as input for proximity searches.
The postcode consists of two parts, usually separated by a space:
- Outward code or outcode
- Inward code or incode
Both parts are geographical: The outcode defines an area and, when combined with the incode, a subset of that area.
For example, for postcode WC2B 4AB the outcode is WC2B, the incode is 4AB, and the area defined by WC2B 4AB falls within the area defined by WC2B.
Further geographical sub-divisions are available beyond just outcode and incode.
These are:
- Postcode area
- Postcode district
- Postcode sector
- Unit postcode
Each division defines an ever-smaller geographical area within the division above.
Taking our example postcode of WC2B 4AB:
Postcode area | WC |
Postcode district | WC2B |
Postcode sector | WC2B 4 |
Unit postcode | WC2B 4AB |
The below graphic shows these areas on a map:
Given a postcode, it is possible to derive each of the subdivisions.
The below graphic shows the how they are represented within the format of the postcode:
You can use a regular expression to parse the postcode into its component parts:
/^(((([A-Z][A-Z]{0,1})[0-9][A-Z0-9]{0,1}) {0,}[0-9])[A-Z]{2})$/
This matches, in order:
- Unit Postcode
- Postcode Sector
- Postcode District
- Postcode Area
You can see a demo of this on regex101
You can also use a dataset like Open Postcode Geo which is available as flat file or API.
Geocoding data for UK postcodes is mainly open, although there are restrictions for Northern Ireland postcodes.
The Ordnance Survey publishes Code-Point Open which provides Easting and Northing.
Open Postcode Geo also has easting and northing, but adds latitude, longitude, postcode area, postcode sector, postcode district and unit postcode.