Skip to content

Instantly share code, notes, and snippets.

@samuelleach
Created February 25, 2016 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelleach/ebd15d4ac53e34ea7b49 to your computer and use it in GitHub Desktop.
Save samuelleach/ebd15d4ac53e34ea7b49 to your computer and use it in GitHub Desktop.
UK postcode regex
UK Postcode Regexes:
- Assumes upper case letters.
- Assumes there is zero or one space between outcode and incode
Postcode
(?:(?:[A-PR-UWYZ][0-9]{1,2}|[A-PR-UWYZ][A-HK-Y][0-9]{1,2}|[A-PR-UWYZ][0-9][A-HJKSTUW]|[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRV-Y]) ?[0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)
Outcode
([A-PR-UWYZ][0-9]{1,2}|[A-PR-UWYZ][A-HK-Y][0-9]{1,2}|[A-PR-UWYZ][0-9][A-HJKSTUW]|[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRV-Y])(?= ?[0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)
Incode
(?![A-PR-UWYZ][0-9]{1,2}|[A-PR-UWYZ][A-HK-Y][0-9]{1,2}|[A-PR-UWYZ][0-9][A-HJKSTUW]|[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRV-Y] ?)([0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)
References
Useful input from http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment