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 Code | Inward Code | ||
---|---|---|---|
Area | District | Sector | Unit |
OX | 26 | 6 | XE |
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.
Format | Total | Examples |
---|---|---|
GIR 0AA | 1 | GIR 0AA |
A1B 2CD | 9,491 | W1A 0AX |
AB1C 2DE | 10,700 | SW1V 4AY, SW1X 7DD, WC2B 6UF |
A1 2BC | 44,512 | M1 1EG, M1 1EE |
A12 3BC | 156,895 | S70 5TB, E17 6PF |
AB1 2CD | 700,231 | CV3 2AS, DN3 1SN |
AB12 3CD | 844,680 | BH10 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.
Length | Total |
---|---|
5 | 44,512 |
6 | 866,618 |
7 | 855,380 |
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 < 5) 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