Regular Expressions 101

Community Patterns

UK Postcode checker

1

Regular Expression
Python

r"
^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$
"

Description

Matches more thoroughly than others in the library. Based on http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive. Matches: W1A 1AA, WC2H 7LT, SE50EG

Submitted by spookypeanut - 9 years ago