iOS Objective-C NSDataDetector的使用

NSDataDetectorNSRegularExpression的子类,主要用于检测半结构化的数据:日期,地址,电话号码,正则表达式等等。

创建方法

+ (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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值