如何从字符串中分离出 Phone 号码和会议代码

How to separate Phone number and Conference Code from String

我有如下格式的字符串,

Call-in toll-free number: 180012345678 (India)

Call-in number: +44-(0)123456789 (United Kingdom)

UK toll free : 12345678910

https://www.google.com/

Conference Code: 123 456 56789

如何将 Phone 数字与 Objective-C

中的此字符串分开

您可以使用 NSDataDetector (https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSDataDetector_Class/)

支持以下类型:

NSTextCheckingTypeOrthography        = 1ULL << 0,
NSTextCheckingTypeSpelling           = 1ULL << 1,
NSTextCheckingTypeGrammar            = 1ULL << 2,
NSTextCheckingTypeDate               = 1ULL << 3,
NSTextCheckingTypeAddress            = 1ULL << 4,
NSTextCheckingTypeLink               = 1ULL << 5,
NSTextCheckingTypeQuote              = 1ULL << 6,
NSTextCheckingTypeDash               = 1ULL << 7,
NSTextCheckingTypeReplacement        = 1ULL << 8,
NSTextCheckingTypeCorrection         = 1ULL << 9,
NSTextCheckingTypeRegularExpression  = 1ULL << 10
NSTextCheckingTypePhoneNumber        = 1ULL << 11,
NSTextCheckingTypeTransitInformation = 1ULL << 12

例如

NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];

NSUInteger numberOfMatches = [detector numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];

NSArray *matches = [detector matchesInString: stringoptions:0 range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match range];
    if ([match resultType] == NSTextCheckingTypePhoneNumber) {
        NSString *phoneNumber = [match phoneNumber];
    }
}

从与您的问题非常相似的另一个线程检查此代码: