0

I've been searching for a global postcode based regex but not found one. Anyone know of one that exists?

buggedcom
  • 1,537
  • 2
  • 18
  • 34
  • 1
    What do you want the regex to actually do? Bearing in mind that a decent regex just for UK postcodes is "((A[BL]|B[ABDHLNRST]?|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]([1-9][0-9]|[2-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP- UW-Z]{2}", it's possible that one for multiple countries could become a tad complicated... (http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom) – Matt Gibson Aug 10 '10 at 13:46

1 Answers1

0

Not sure if this really makes sense to try to test for, I think it would be: 0-18 characters which can contain: 0-9 + the basic Latin alphabet + spaces, hyphens, parentheses and a Cyrillic letter.

And I'm not at all sure that even that would cover all possibilities.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • @Amarghosh: But I think I've seen that people sometimes put a space in the middle? And if that's correct, then even that simple regex becomes a bit more complex (though I'm not sure if spaces are required in any country so possibly you could just strip out all spaces before doing the match). – Hans Olsson Aug 10 '10 at 13:53
  • People put spaces while writing (for ease of reading I guess), but that's not the standard. But I agree that an all encompassing regex for any post code will be cumbersome. – Amarghosh Aug 11 '10 at 03:26
  • Spaces are part of the format of UK postcodes. For example, "BS1 6XB". The space isn't generally considered as optional, so they're almost always written like that. – Matt Gibson Aug 11 '10 at 10:20