NSDataDetector
是NSRegularExpression的子类,主要用于检测半结构化的数据:日期,地址,电话号码,正则表达式等等。
创建方法
+ (nullable NSDataDetector *)dataDetectorWithTypes:(NSTextCheckingTypes)checkingTypes error:(NSError **)error;
- (nullable instancetype)initWithTypes:(NSTextCheckingTypes)checkingTypes error:(NSError **)error;
NSTextCheckingType
参数
typedef NS_OPTIONS(uint64_t, NSTextCheckingType) { // a single type
NSTextCheckingTypeOrthography = 1ULL << 0, // language identification
NSTextCheckingTypeSpelling = 1ULL << 1, // spell checking
NSTextCheckingTypeGrammar = 1ULL << 2, // grammar checking
NSTextCheckingTypeDate = 1ULL << 3, // date/time detection
NSTextCheckingTypeAddress = 1ULL << 4, // address detection
NSTextCheckingTypeLink = 1ULL << 5, // link detection
NSTextCheckingTypeQuote = 1ULL << 6, // smart quotes
NSTextCheckingTypeDash = 1ULL << 7, // smart dashes
NSTextCheckingTypeReplacement = 1ULL << 8, // fixed replacements, such as copyright symbol for (c)
NSTextCheckingTypeCorrection = 1ULL << 9, // autocorrection
NSTextCheckingTypeRegularExpression = 1ULL << 10, // regular expression matches
NSTextCheckingTypePhoneNumber = 1ULL << 11, // phone number detection
NSTextCheckingTypeTransitInformation = 1ULL << 12 // transit (e.g. flight) info detection
};
NSDataDetector
可以调用NSRegularExpression
的方法进行校验,
示例代码
NSTextCheckingTypes type = NSTextCheckingTypeDate|NSTextCheckingTypePhoneNumber|NSTextCheckingTypeLink;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:type error:nil];
NSString *detectorStr = @"姓名: 张三 出生日期: 19900308 电话: 13712345678 邮件: zhangsan@126.com 地址: 上海市浦东新区";
// 3
NSLog(@"%ld", [detector numberOfMatchesInString:detectorStr options:0 range:NSMakeRange(0, detectorStr.length)]);
// 19900308
NSTextCheckingResult *result = [detector firstMatchInString:detectorStr options:0 range:NSMakeRange(0, detectorStr.length)];
if (result.range.location != NSNotFound) {
NSLog(@"firstMatchInString result: %@", [detectorStr substringWithRange:result.range]);
}
// 19900308
NSRange range = [detector rangeOfFirstMatchInString:detectorStr options:0 range:NSMakeRange(0, detectorStr.length)];
if (range.location != NSNotFound) {
NSLog(@"rangeOfFirstMatchInString result: %@", [detectorStr substringWithRange:range]);
}
// 19900308
// 13712345678
// zhangsan@126.com
NSArray *results = [detector matchesInString:detectorStr options:0 range:NSMakeRange(0, detectorStr.length)];
for (NSTextCheckingResult *result in results) {
NSLog(@"matchesInString result: %@", [detectorStr substringWithRange:result.range]);
}
// date = 19900308
// phone = 13712345678
// link = zhangsan@126.com
[detector enumerateMatchesInString:detectorStr options:0 range:NSMakeRange(0, detectorStr.length)
usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
if (result.resultType == NSTextCheckingTypeDate) {
NSLog(@"date = %@", [detectorStr substringWithRange:result.range]);
} else if (result.resultType == NSTextCheckingTypePhoneNumber) {
NSLog(@"phone = %@", [detectorStr substringWithRange:result.range]);
} else if (result.resultType == NSTextCheckingTypeLink) {
NSLog(@"link = %@", [detectorStr substringWithRange:result.range]);
}
}];
输出
3
firstMatchInString result: 19900308
rangeOfFirstMatchInString result: 19900308
matchesInString result: 19900308
matchesInString result: 13712345678
matchesInString result: zhangsan@126.com
date = 19900308
phone = 13712345678
link = zhangsan@126.com