Regular Expressions 101

Community Patterns

UK Full or Partial Postcode Validate, Match or Breakdown

0

Regular Expression
PCRE (PHP <7.3)

/
^(?<postcode>|(?<outward>(?<area>\w{1,2}))(?<district>\d\w|\d{1,2}))(?:\s)?(?<inward>(?<sector>\d)(?<unit>\w\w))?
/
gmi

Description

Matches all standard UK postcodes whether partial (outward) or full (outward and inward), spaces optional. To enforce spaces remove the ? after (?:\s). There are a few letters not currently in use, this test does not seek to exclude them.

The only known postcode this will not validate for is GIR 0AA the special sort code created for Girobank.

Submitted by anonymous - 6 years ago