您也可以使用正则表达式来捕获数字(假设结果中只有数字字符和空格,并且总是有“会议编号:”前缀):
NSString *text = @"Please plan to attend to provide upgrade to existing code\nmorning meeting to acoomdate bum team members\nMeeting Number: 457 231 123\nTo join this meeting\n\ngo to http://domainname.com\nenter password";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Meeting Number: ([0-9 ]+)" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *result = [text substringWithRange:matchRange]; // Here is the result
}
这可以用来一起解析多个输入,因为我们这里有一个循环。如果要求只是解析一个输入,请将正则表达式声明后的代码替换为:
NSTextCheckingResult *match = [regex firstMatchInString:text
options:0
range:NSMakeRange(0, [text length])];
if (match) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *result = [text substringWithRange:matchRange];
}