匹配手机号码
NSString *str = @"";
NSString *pattern = @"(13\\d|14[579]|15[^4\\D]|17[^49\\D]|18\\d)\\d{8}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: nil error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
if (match.count != 0) {
// ...
}
匹配邮箱
NSString *str = @"";
NSString *pattern = @"\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: nil error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
if (match.count != 0) {
// ...
}
匹配身份证
NSString *str = @"";
NSString *pattern = @"\\d{17}[0-9Xx]|\\d{15}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: nil error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
if (match.count != 0) {
// ...
}