How complicated is a postcode?

In the UK we take postcodes for granted. A relatively simple set of characters and numbers which narrow the UK down to a very small area of addresses, in theory but when you try to process a postcode programmatically it gets very complicated very quickly.

A UK postcode is made up of five to seven alphanumeric characters split into two distinct sections—the outward code and the inward code—separated by a single space or sometimes not depending how it’s written. These parts represent a hierarchical system ranging from broad areas down to individual streets or properties.

Outward Code (Outcode)

The first part of the postcode allows mail to be sent to the correct local area for sorting. It can be two to four characters long. 
Postcode Area: One or two letters identifying a region (e.g., ‘SW’ for South West London, ‘B’ for Birmingham).
Postcode District: One or two digits (sometimes followed by a letter in London) identifying a smaller district within the main postcode area (e.g., ‘SW1A’). 

Inward Code (Incode)

The second half of the postcode is always three characters long. 
Postcode Sector: A single digit indicating a further subdivision within the district.
Postcode Unit: The final two letters, which define a street, part of a street, a single address, or a large organization. 
Example: SW1A 0AA (Buckingham Palace)
SW: Area
1A: District
0: Sector
AA: Unit

Key Facts About Postcode Structure

  • Hierarchical: Works from largest geographic area (Area) to smallest (Unit).
  • Average Coverage: A single, full postcode typically covers about 15 properties.
  • Characters Used: Uses digits (0–9) and letters (A–Z), though certain letters are avoided for clarity in specific positions.
  • Non-Geographic: Some postcodes are assigned to massive companies for high-volume mail, even if they aren’t tied to a specific physical location. 

At Bikes Services UK, we work on postcode districts to determine what Zone (collection of postcode districts) your delivery falls into which, in turn, allows us to calculate a postcode surcharge where appropriate.

What customers don’t see is how hard it is to strip the postcode district out of a postcode.

We use a regular expression which, in 99% occasions can successfully strip a district out of a postcode with a space in it or not. For the few outliers where this isn’t possible, adding the space in usually makes the process work.

This is the regular expression we use. 

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

I think you’ll agree, postcodes are not as simple as they look when t comes to processing them