Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^([Gg][Ii][Rr] 0[Aa]{2})|((?:(?:[A-Za-z][0-9]{1,2})|(?:(?:[A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(?:(?:[A-Za-z][0-9][A-Za-z])|(?:[A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
/
gm

Description

This is intended to be a relatively simple matching (anchored left and right) for a uk post code.

Please use the unit tests to run the examples. The examples are from wikipedia, but I shall try and keep it up to date.

Submitted by anonymous - 4 years ago