简体   繁体   English

检查邮政编码是否在范围内

[英]Check if postal code is in a range

this might be a dumb question but I'm new to iOS programming and I just couldn't think of anything. 这可能是一个愚蠢的问题,但我是iOS编程的新手,我什么都没想到。 for my app I need to check if a certain postal code is in a range. 对于我的应用程序,我需要检查某个邮政编码是否在范围内。 It's easy to do for German or US codes which are plain numbers, but now I have to do it for canadian. 对于德国或美国的纯数字代码来说,这样做很容易,但是现在我必须对加拿大人这样做。

Example canadian postal code looks like this: A0A, J3X, R7N, V1M. 加拿大邮政编码示例如下所示:A0A,J3X,R7N,V1M。

Is there a way to do this? 有没有办法做到这一点?

Thanks for any help! 谢谢你的帮助!

You can use a regex like this: 您可以使用以下正则表达式:

^(A0A|J3X|R7N|V1M)$

If you want ranges you can do something like this: 如果需要范围,可以执行以下操作:

^(A0A|J3X|R7N|V1[M-Z])$
                 ^--- Will match V1M until V1Z

Another example for range V1Z could be: 范围V1Z的另一个示例可能是:

^(A0A|J3X|R7N|V[5-9][M-Z])$
                 ^--- Will match VxM until VxZ and V5x until V9x

If you just want a digit you can use \\d (it's the shortcut for [0-9] ). 如果只需要一个数字,则可以使用\\d (这是[0-9]的快捷方式)。 Same for \\w that means [A-Za-z0-9_] . \\w相同,表示[A-Za-z0-9_]

You can specify ranges using regex classes. 您可以使用正则表达式类指定范围。 Like: 喜欢:

[0-4]
[7-9]
[A-M]
[O-X]

This answer on SO has a good description of how regular expression character class ranges work. 关于SO的答案很好地描述了正则表达式字符类范围是如何工作的。 To paraphrase: you aren't just limited to letters and numbers ( [AZ] or [0-9] ), but you should be careful attempting any more complicated ranges. 释义: 您不仅限于字母和数字( [AZ][0-9] ),而且在尝试任何更复杂的范围时都应小心。

A range will allow for any character between, according to the list of ASCII characters , the start and end characters to match. 一个范围将允许根据ASCII字符列表在开始和结束字符之间进行匹配的任何字符。 This means any range is technically valid (you may just see odd results). 这意味着任何范围在技术上都是有效的(您可能会看到奇怪的结果)。 For example, these two classes are the same: 例如,这两个类是相同的:

[0-Z]
[0-9:;<=>?@A-Z]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM