8

I'm looking to be able to validate UK Postcodes, and ideally, I would like the following cases to pass:

  1. W1
  2. W12
  3. WC1
  4. WC1A
  5. WC12
  6. W1 6BT
  7. W12 6BT
  8. WC1 6BT
  9. WC1A 6BT
  10. WC12 6BT
  11. W16BT
  12. W126BT
  13. WC16BT
  14. WC1A6BT
  15. WC126BT

I have the following regex patterns:

^(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9])( [0-9][ABD-HJLNP-UW-Z]{2})?)$

This pattern allows for 3 or 4 & 6 or 7 digit postcodes (so either outward code only with 3 or 4 digits, or full postcodes with 6 or 7 digits) however it doesn't allow for points 4 and 6 (postcodes where spaces have been omitted)

I also have this pattern:

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

This pattern allows for 6 or 7 digit postcodes, with our without the space, but not for incomplete postcodes (outward code only)

Sorry for asking a question that has been covered so extensively already on here, but all the examples I found, they match part of my requirement, but not all of it.

Ideally, I'd like a regex pattern that allows 3, 4, 6 & 7 digit postcodes, with our without spaces.

UPDATE:

I've re done my pass cases as I don't think it was entirely comprehensive initially. The basic concept that is it should follow UK postcode patterns, and validate any of the following combinations:

1 Letter 1 Number
1 Letter 2 Numbers
2 Letters 1 Number
2 Letters 1 Number 1 Letter
2 Letters 2 Numbers
1 Letter 1 Number (OptionalSpace) 1 Number 2 Letters
1 Letter 2 Numbers (OptionalSpace) 1 Number 2 Letters
2 Letters 1 Number (OptionalSpace) 1 Number 2 Letters
2 Letters 1 Number 1 Letter (OptionalSpace) 1 Number 2 Letters
2 Letters 2 Numbers (OptionalSpace) 1 Number 2 Letters

^ Hope the above makes sense, and is detailed enough. Bit hard to read I know.

ANSWER:

So I now have a regex that passes all the cases above (example and pattern). As mentioned in the comment below, it's very hard, if not impossible to cater for ALL UK postcodes, nevertheless, the one below does all I need and should be good for 90% of input cases:

^(GIR 0AA)|[a-z-[qvx]](?:\d|\d{2}|[a-z-[qvx]]\d|[a-z-[qvx]]\d[a-z-[qvx]]|[a-z-[qvx]]\d{2})(?:\s?\d[a-z-[qvx]]{2})?$
JustinMoser
  • 545
  • 2
  • 6
  • 25
  • Take a look at this [question](http://stackoverflow.com/q/15960184/) and it's answers for some inspiration. Also don't worry too much about spaces, you could just strip them to ease up the process ! – HamZa Jun 09 '13 at 18:24
  • Ah see, thats the issue, the top one only allows postcodes WITH spaces. If it was the other way around I would definitely just strip any with spaces. But figuring where a space should go for those without is a little harder. Thanks though! :) – JustinMoser Jun 10 '13 at 10:23
  • 1
    Any attempt to do this is bound to fail. There is no such thing as a regular expression to validate a UK postcode and numerous special cases for instance Girobank, GIR 0AA, and the overseas territories, the entirety of the BFPO, [though this is now changing](https://www.gov.uk/government/publications/british-forces-post-office-locations). Equally, plenty of countries use the same style, which can cause massive problems with validation. The simplest thing to do is to create a look-up with some official data like code point open: http://www.ordnancesurvey.co.uk/oswebsite/products/code-point-open – Ben Jun 10 '13 at 11:53
  • @Ben Thanks for that. Yeah I think you're right. I have a regex patterm that satisfies all the patters/examples above, but would still fall short where you mentioned (GiroBank). Luckily, it isn't an international site, so as long as it validates the examples above, it should be fine. I'll post what I have. – JustinMoser Jun 10 '13 at 11:59
  • 1
    I've extended my comment into [an answer](http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive/17024047#17024047) on the linked question @Justin. Maybe I'll save someone :-). – Ben Jun 10 '13 at 12:47
  • @JustinMoser I had pretty much the same requirement as you, but also needed to extract the out and in (optional) codes so that I could put them back together with the space in the middle and search against a database. Basically parsing a search term entered by a user. I went with this: `(?[A-Z]{1,2}(\d(?=\ ?\d[A-Z])|\d\d|\d[A-Z]?))\ ?(?\d[A-Z]{0,2})?` – DigitalDan Mar 23 '16 at 17:05

1 Answers1

7

Did you note that related question UK Postcode Regex (Comprehensive)?

The RegEx supplied by the UK Government was:

(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})

As pointed out on the Wikipedia discussion, this will allow some non-real postcodes (e.g. those starting AA, ZY) and they do provide a more rigorous test that you could try.

Community
  • 1
  • 1
rekire
  • 47,260
  • 30
  • 167
  • 264
  • The regex marked as the answer does not allow for outward code only postcodes or full postcodes without a space. "SW11 9PT" will validate but not "SW12", "CR5","SW129PT" or "CR52EP". The comment on that answer solves the space problem but still does not allow for outward code with 3 or 4 digits only. I did see that answer before posting, but none of the suggestions do it all, as the question says. Thanks anyway :) – JustinMoser Jun 10 '13 at 10:46
  • I have tried every regex on that page, and none work totally. 3 and 4 digit outer only (containing letters and numbers), 6 or 7 full (with and without spaces) should validate. I dont mind whether it's a VALID postcode, I can do manual checking against that after the fact, I just need to make sure it recognises that it is a postcode that's been entered rather than a town/area name. – JustinMoser Jun 10 '13 at 10:59
  • I seems your Regex is incorrect, according to the question you have linked and Wikipedia: "The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with A9A." In your answer the P is missing! – LeBaptiste Feb 07 '17 at 16:09
  • @leBap as you may have noticed my answer is 3.5 years old. So my answer could be outdated. I'll check and update it when I find some free time. – rekire Feb 07 '17 at 18:36
  • @rekire I effectively noticed it was an old post but since this answer is still viewed a lot I wanted to stress this little difference in case people uses your regex, not a reproach of course. Note there are some UK Government resources with a much more permissive postcode format than the rules presented in Wikipedia: https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/488478/Bulk_Data_Transfer_-_additional_validation_valid_from_12_November_2015.pdf – LeBaptiste Feb 08 '17 at 10:25