Regular Expressions 101

Community Patterns

UK Postcode

1

Regular Expression
PCRE (PHP <7.3)

/
(^\s?[^qvx][^ijz]?[0-9]{1,2}[A,B,C,D,E,F,G,H,J,K,S,T,U,W]?\s?[0-9][^CIKMOV]{2}\s?)
/
gmi

Description

as per the following specifications:

FORMAT EXAMPLE AN NAA M1 1AA ANN NAA M60 1NW AAN NAA CR2 6XH AANN NAA DN55 1PT ANA NAA W1A 1HQ AANA NAA EC1A 1BB

  • The letters Q, V and X are not used in the first position
  • The letters I,J and Z are not used in the second position.
  • The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.
  • The second half of the postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used.
Submitted by Spluf (Cosmin Pop) - 8 years ago