Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
Created·2018-08-14 15:45
Flavor·PCRE (Legacy)

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

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