I've been searching for a global postcode based regex but not found one. Anyone know of one that exists?
A:
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.
ho1
2010-08-10 13:34:27
Indian post codes are just six digits. `^\d{6}$`
Amarghosh
2010-08-10 13:43:17
@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).
ho1
2010-08-10 13:53:12
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
2010-08-11 03:26:01
@ho1 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
2010-08-11 10:20:43