//手机号码验证
- (BOOL) validateMobile:(NSString *)mobile{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(14[0,0-9])|(17[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//筛选手机的函数
-(NSMutableArray *)getMobileFromAllStr:(NSString *)newStr{
NSString *needMobile = @"";
NSMutableArray *arrMobile = [[NSMutableArray alloc] init];
for (int i = 0; i<newStr.length; i++) {
NSString *temp = [newStr substringWithRange:NSMakeRange(i,1)];
if ([temp isEqualToString:@"1"]) {
NSInteger k = i;
if (k+11<=newStr.length) {
needMobile = [newStr substringWithRange:NSMakeRange(k, 11)];
if ([self validateMobile:needMobile]) {
NSLog(@"needMobile:%@",needMobile);
[arrMobile addObject:needMobile];
}
}
}
}
return arrMobile;
}
iOS 字符串筛选手机号
最新推荐文章于 2021-11-30 11:26:28 发布