如何使用 + 验证电话号码目标c中的符号?

发布于 2024-10-21 20:18:37 字数 88 浏览 1 评论 0原文

我对正则表达式方法很困惑。我的要求是验证前缀中可能包含 + 符号的电话号码。那么所有字符都应该只是数字。为此,我如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦行僧 2024-10-28 20:18:37

我回答晚了,但当我最近遇到同样的问题时,我发现了一个有趣的解决方案。它使用内置的可可方法而不是自定义正则表达式。

- (BOOL)validatePhoneNumberWithString:(NSString *)string {
    if (nil == string || ([string length] < 2 ) )
        return NO;

    NSError *error;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
    NSArray *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    for (NSTextCheckingResult *match in matches) {
        if ([match resultType] == NSTextCheckingTypePhoneNumber) {
            NSString *phoneNumber = [match phoneNumber];
            if ([string isEqualToString:phoneNumber]) {
                return YES;
            }
        }
    }

    return NO;
}

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.

- (BOOL)validatePhoneNumberWithString:(NSString *)string {
    if (nil == string || ([string length] < 2 ) )
        return NO;

    NSError *error;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
    NSArray *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    for (NSTextCheckingResult *match in matches) {
        if ([match resultType] == NSTextCheckingTypePhoneNumber) {
            NSString *phoneNumber = [match phoneNumber];
            if ([string isEqualToString:phoneNumber]) {
                return YES;
            }
        }
    }

    return NO;
}
拍不死你 2024-10-28 20:18:37

我不会说这是一个明确的答案,但它应该给你一个开始。

^\x2b[0-9]+

将匹配以“+”开头的任何字符串,然后是任何数量大于 0 的数字。

例如:

+441312002000  - Full phone number matched.
+4413120c2000  - +4413120 is matched.
++441312002000 - No match 
441312002000   - No Match

如果对长度等有进一步的限制,则指定,我可以更新正则表达式。我同意其他关于使用 RegexKitLite 的海报。

I wouldn't say this is a definitive answer but it should give you a start.

^\x2b[0-9]+

Will match any string that starts with a '+' and then any amount of numbers greater than 0.

For instance:

+441312002000  - Full phone number matched.
+4413120c2000  - +4413120 is matched.
++441312002000 - No match 
441312002000   - No Match

If there are further constraints on length etc then specifiy and I can update the regex. I agree with other poster about using RegexKitLite.

失眠症患者 2024-10-28 20:18:37

使用RegexKitLite,检查以下http://regexkit.sourceforge.net/RegexKitLite/

Use RegexKitLite, check the following http://regexkit.sourceforge.net/RegexKitLite/

天邊彩虹 2024-10-28 20:18:37
^\+?[0-9]*$

应该这样做:

^      # start of string
\+?    # match zero or one + characters
[0-9]* # match any number of digits
$      # end of string

要在字符串中使用正则表达式,您需要将反斜杠加倍: @"^\\+?[0-9]*$" 应该根据我的其他正则表达式示例进行工作见过,但我不了解 Objective-C,对此可能是错误的。

^\+?[0-9]*$

should do:

^      # start of string
\+?    # match zero or one + characters
[0-9]* # match any number of digits
$      # end of string

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.

日记撕了你也走了 2024-10-28 20:18:37

这篇文章很好地解释了正则表达式 - http://blog.stevenlevithan.com/archives/validate -电话号码。您必须使用“\”而不是“\”来防止 Objective C 预处理器将正则表达式转义码解释为字符串转义码。

这是您将用于请求的匹配的 NSString

NSString *northAmRegexWithOptionalLeadingOne = @"^(?:\\+?1[-. ]?)?\\(?([2-9][0-8][0-9])\\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$";

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

NSString *northAmRegexWithOptionalLeadingOne = @"^(?:\\+?1[-. ]?)?\\(?([2-9][0-8][0-9])\\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$";
度的依靠╰つ 2024-10-28 20:18:37

+*[0-9]{手机长度}。应该有效。

+*[0-9]{length of phone}. Should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文