iPhone 判断字符串是否为英国邮政编码
在我的应用程序中,在发送字符串之前,我需要确定文本框中输入的文本是否是英国邮政编码。我没有正则表达式的能力来为自己解决这个问题,在四处搜索后我似乎无法解决这个问题!只是想知道过去是否有人做过类似的事情?
或者,如果有人能指出我正确的方向,我将不胜感激!
汤姆
In my app before I send a string off I need to work out if the text entered in the textbox is a UK Postcode. I don't have the regex ability to work that out for myself and after searching around I can't seem to work it out! Just wondered if anyone has done a similar thing in the past?
Or if anyone can point me in the right direction I would be most appreciative!
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
维基百科有一个关于此的好部分。基本上,答案取决于您想要处理哪种病理病例。例如:
基本上,仔细阅读维基百科的该部分并决定您需要什么。
Wikipedia has a good section about this. Basically the answer depends on what sort of pathological cases you want to handle. For example:
Basically, read that section of Wikipedia thoroughly and decide what you need.
对于没有空格的邮政编码(例如 SE19QZ),我使用:(它还没有让我失望;-))
如果有空格(例如 SE1 9QZ),那么:
for post codes without spaces (e.g. SE19QZ) I use: (its not failed me yet ;-) )
if spaces (e.g. SE1 9QZ) , then:
您可以使用此正则表达式匹配大多数邮政编码:
这意味着.. .
AZ
一次或两次 ({1,2}
),后跟 1 次或两次0-9
,后跟一个空格\s
可选?
后跟0-9
一两次,后跟AZ
一两次。这会匹配一些误报,因为我可以编写像
ZZ00 00ZZ
这样的邮政编码,但要准确匹配所有邮政编码,唯一的方法是从邮局购买邮政编码数据 - 这相当昂贵的。您还可以下载免费的邮政编码数据库,但它们的覆盖率不是 100%。希望这有帮助。
You can match most post codes with this regex:
Which means...
A-Z
one or two times ({1,2}
) followed by0-9
1 or two times, followed by a space\s
optionally?
followed by0-9
one or two times, followed byA-Z
one or two times.This will match some false positives, as I can make up post codes like
ZZ00 00ZZ
, but to accurately match all post codes, the only way is to buy post code data from the post office - which is quite expensive. You could also download free post code databases, but they do not have 100% coverage.Hope this helps.
维基百科有一些英国邮政编码的正则表达式: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
Wikipedia has some regexes for UK Postcodes: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation