For the USA, you have quantifiers ( {5} , {4} ? ) Correctly, but you forgot to indicate exactly what you are quantifying. Do you want to:
(^[0-9]{5}(-[0-9]{4})?$)
In Canada, according to Wikipedia, the format is A0A 0A0 , so I would do:
(^[a-zA-Z][0-9][a-zA-Z][- ]*[0-9][a-zA-Z][0-9]$)
Now I would write a complete expression like this with case sensitivity turned on:
@"^(\\d{5}(-\\d{4})?|[az]\\d[az][- ]*\\d[az]\\d)$"
Honestly, I'm really not familiar with Objective-C or iOS, and unfortunately, I have not tested the above. However, I have previously seen that such messages mention NSRegularExpression , which is missing from your code but may not be needed. Take a look at other examples to see what other simple mistakes you could make. Good luck.
source share