I am looking for official/reliable regexps to extract sector/district/areas from a UK postcode. I have found a few online but I am not sure how good they are, and I have not a way to test them properly. Do you know any of those regex that can be considered valid?

1 Answer 1

/^(((([A-Z][A-Z]{0,1})[0-9][A-Z0-9]{0,1}) {0,}[0-9])[A-Z]{2})$/

This matches, in order:

  • Unit Postcode
  • Postcode Sector
  • Postcode District
  • Postcode Area

I have a page on my website which addresses postcode format and using regular expressions to parse a post code into it's constituent parts.

  • I am using exactly the same regexp at the moment, that I got from another website
    – Randomize
    Commented Sep 27, 2018 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.