- 正則表達式只匹配數字
- 從匹配數字開始位置,長度正好為11位的提取出來
NSString *string;
NSString *pattern;
pattern=@"\\d*";
string=@"2017-02-12 上報人:張三 15930384756";
NSError *error;
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSLog(@"%@",error);
[regex enumerateMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
if (NSMatchingReportProgress==flags) {
}else{
/**
* 系統內置方法
*/
if (NSTextCheckingTypePhoneNumber==result.resultType) {
NSLog(@"%@",[string substringWithRange:result.range]);
}
/**
* 長度為11位的數字串
*/
if (result.range.length==11) {
NSLog(@"%@",[string substringWithRange:result.range]);
}
}
}];