问题描述
问题:如何编写一个函数来检查并返回一个字符串(NSString)是否包含全球有效的邮政编码。
Question: How would one write a function to check and return whether or not a string (NSString) contains a valid zip code worldwide.
其他信息:我知道iOS中的RegEx。但是我不是那么流利。请记住,这应该接受在任何国家/地区都有效的任何内容。
Additional info: I am aware of RegEx in iOS. However I am not so fluent at it. Please keep in mind this should accepts anything valid in any country as true.
示例
美国 - 10200
美国 - 33701-4313
US - "33701-4313"
加拿大 - K8N 5W6
Canada - "K8N 5W6"
英国 - 3252-322
UK - "3252-322"
等。
编辑那些谁投票或关闭这个问题,请提及为什么。谢谢。
Those who voted down or to close the question, please do mention why. Thank you.
推荐答案
^ [ABCEGHJKLMNPRSTVXY] \d [AZ] [ - ] * \ d [AZ] \d $
匹配带或不带空格的加拿大邮政编码格式(例如T2X 1V4或T2X1V4)
Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")
^ \d {5}( - \d {4})?$
匹配所有美国格式的邮政编码格式(例如,94105-0011或94105)
Matches all US format ZIP code formats (e.g., "94105-0011" or "94105")
(^ \d {5}( - \d {4})?$)|(^ [ABCEGHJKLMNPRSTVXY] \ [AZ] [ - ] * \d [AZ] \d $)
匹配上面的美国或加拿大代码格式。
Matches US or Canadian codes in above formats.
英国代码比你想象的要复杂得多:
UK codes are more complicated than you think: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
这篇关于iOS:有效的邮政编码检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!