Regular Expressions Useful Regex Showcase Match UK postcode


Example

Regex to match postcodes in UK

The format is as follows, where A signifies a letter and 9 a digit:

FormatCoverageExample
CellCell
AA9A 9AAWC postcode area; EC1–EC4, NW1W, SE1P, SW1EC1A 1BB
A9A 9AAE1W, N1C, N1PW1A 0AX
A9 9AA, A99 9AAB, E, G, L, M, N, S, WM1 1AE, B33 8TH
AA9 9AA, AA99 9AAAll other postcodesCR2 6XH, DN55 1PT
(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-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})

Where first part:

 (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-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY]))))

Second:

[0-9][A-Z-[CIKMOV]]{2})