Auto-Formatting UK Postcodes and validation


If you were to ask a user to provide a postcode little or no validation on, you would probably find that a postcode such as OX26 6XE would end up as something like Ox266xe or ox2 66xe.

For some applications, this maybe acceptable. But for others, especially when integrating with address APIs, formatting maybe crucial.

When I looked for a regular expression for UK postcodes, I found many variations that permitted or denied certain combinations but never something definitive.

I decided to look into the UK postcode system for myself and see what formatting combinations are used.

This post maybe particularly useful for those testing client systems in the Quality Assurance department. 🙂


Background Research

The UK postcode system was originally devised by Royal Mail and was rolled out nationally between 1959 and 1974. To this day it is still owned by Royal Mail despite being privatised in 2015.

UK postcode are alphanumeric, and range between six to eight characters.

The postcode format is typically split into two parts; an outward code followed by an inward code.

Outward CodeInward Code


With the help of an open source database of UK postcodes, I began to compile a definitive list of the different formats used. FreeMapTools

By writing some SQL against the database, I was able to categorise each of the formats and where the spacing is used.

GIR 0AA: This postcode is the delivery address for Girobank (which is now part of Santander). Girobank (Santander) in Bootle.


A1B 2CD9,491W1A 0AX
AB1C 2DE10,700SW1V 4AY, SW1X 7DD, WC2B 6UF
A1 2BC44,512M1 1EG, M1 1EE
A12 3BC156,895S70 5TB, E17 6PF
AB1 2CD700,231CV3 2AS, DN3 1SN
AB12 3CD844,680BH10 5BH, ME99 2AH

Total Postcodes: 1,766,510. Needless to say, this figure is probably out of date already!


If these postcodes were grouped together by their length, we would have the following outcome. The total does not include the space.



Format Correction

The C# method below can be used to reformat a postcode into the correct spacing.

public static string FormatPostcode(string postcode)
	if (string.IsNullOrWhiteSpace(postcode)) return postcode;
	string pc = Regex.Replace(postcode, "\\s", string.Empty).ToUpper();

	// Only reformat if it is alphanumeric
	var regEx = new Regex("^[a-zA-Z0-9]*$");
	if (!regEx.IsMatch(pc)) return postcode;

	int length = pc.Length, rightStart = length - 3;

	if (length > 7 || length < 6) return postcode; // Invalid postcode, we can't reformat!
	return pc.Insert(rightStart, "\u0020"); // Insert space before last three characters.

For example, the above method will reformat 'Ox261st' into 'OX26 1ST'.


Regular Expression

UK Government recommended expression. However, the expression below allows for none spacing of outer and inner codes.

^(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})$


Post Categories