iOS

  • 正则表达式只匹配数字
  • 从匹配数字开始位置,长度正好为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]);
            }
        }
    }];
原文地址:https://www.cnblogs.com/adampei-bobo/p/8375931.html