如何使用 + 验证电话号码目标c中的符号?
我对正则表达式方法很困惑。我的要求是验证前缀中可能包含 + 符号的电话号码。那么所有字符都应该只是数字。为此,我如何在 Objective C 中创建正则表达式。
I am so confused about the regex methods. My requirement is to validate a phone number that may contains + symbol in its prefix. Then all the charactors should be numerals only. For this, how can i create a regular expression in objective c.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我回答晚了,但当我最近遇到同样的问题时,我发现了一个有趣的解决方案。它使用内置的可可方法而不是自定义正则表达式。
I'm late answering, but I found an interesting solution when I recently have had the same problem. It uses the built-in cocoa methods instead of custom regex.
我不会说这是一个明确的答案,但它应该给你一个开始。
将匹配以“+”开头的任何字符串,然后是任何数量大于 0 的数字。
例如:
如果对长度等有进一步的限制,则指定,我可以更新正则表达式。我同意其他关于使用 RegexKitLite 的海报。
I wouldn't say this is a definitive answer but it should give you a start.
Will match any string that starts with a '+' and then any amount of numbers greater than 0.
For instance:
If there are further constraints on length etc then specifiy and I can update the regex. I agree with other poster about using RegexKitLite.
使用RegexKitLite,检查以下http://regexkit.sourceforge.net/RegexKitLite/
Use RegexKitLite, check the following http://regexkit.sourceforge.net/RegexKitLite/
应该这样做:
要在字符串中使用正则表达式,您需要将反斜杠加倍:
@"^\\+?[0-9]*$"
应该根据我的其他正则表达式示例进行工作见过,但我不了解 Objective-C,对此可能是错误的。should do:
To use the regex in a string, you'll need to double the backslashes:
@"^\\+?[0-9]*$"
should work according to other regex examples I've seen, but I don't know Objective-C and may be wrong about this.这篇文章很好地解释了正则表达式 - http://blog.stevenlevithan.com/archives/validate -电话号码。您必须使用“\”而不是“\”来防止 Objective C 预处理器将正则表达式转义码解释为字符串转义码。
这是您将用于请求的匹配的 NSString
This post nicely explains the regex -- http://blog.stevenlevithan.com/archives/validate-phone-number. You have to use "\" instead of "\" to prevent the Objective C preprocessor from interpreting regex escape codes as character string escape codes.
Here is the NSString you would use for the requested match
+*[0-9]{手机长度}
。应该有效。+*[0-9]{length of phone}
. Should work.