iOS常用正则表达式

2017-02-21  本文已影响90人  RunnerFL

摘自http://blog.csdn.net/xlawszero/article/details/52053184
旧的正则表达式代码:

新版正则表达式代码:

NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

if (([regextestmobile evaluateWithObject:mobileNum] == YES)
    || ([regextestcm evaluateWithObject:mobileNum] == YES)
    || ([regextestct evaluateWithObject:mobileNum] == YES)
    || ([regextestcu evaluateWithObject:mobileNum] == YES))
{
    return YES;
}
else
{
    return NO;
}

}

下面我们简单拆分上面方法,来应对不同的需求

如果只是简单匹配是否是 手机号码,并不需要上面那么多行代码,可以简单写成这样:

NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [regextestmobile evaluateWithObject:mobileNum];

如果你需要匹配是否是 移动/联通/电信 手机号。
判断移动手机号就是这样:

相信细心的朋友,应该已经明白了。判断联通手机号,只要把我们的正则字符串改成上面判断联通手机号的字符串就可以了。判断哪种就改变正则表达式就可以了

ok,在这个基础上,我们还可以组合来判断具体是哪个运营商的手机号,代码如下:

主要方法都已公布,至于Swift或者其他语言代码块就不一一放上来了,真让我都写,我也不一定都会。。。哈哈哈,核心正则表达式,都是可以用的,拿着代入进去就ok了。

摘自:http://www.cnblogs.com/littlesnail/p/6049705.html

import "NSString+RegexCategory.h"

@implementation NSString (RegexCategory)

pragma mark - 正则相关

pragma mark -

//手机号分服务商

/**
 25         * 大陆地区固话及小灵通
 26         * 区号:010,020,021,022,023,024,025,027,028,029
 27         * 号码:七位或八位
 28         */
NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";


//    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];

if (([self isValidateByRegex:CM])
    || ([self isValidateByRegex:CU])
    || ([self isValidateByRegex:CT])
    || ([self isValidateByRegex:PHS]))
{
    return YES;
}
else
{
    return NO;
}

}

//手机号有效性

//邮箱

//身份证号

//车牌

pragma mark - 算法相关

//精确的身份证号码有效性检测

NSRegularExpression *regularExpression;
NSUInteger numberofMatch;

int year =0;
switch (length) {
    case 15:
        year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
        
        if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
            
            regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                                                     options:NSRegularExpressionCaseInsensitive
                                                                       error:nil];//测试出生日期的合法性
        }else {
            regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                                                    options:NSRegularExpressionCaseInsensitive
                                                                      error:nil];//测试出生日期的合法性
        }
        numberofMatch = [regularExpression numberOfMatchesInString:value
                                                           options:NSMatchingReportProgress
                                                             range:NSMakeRange(0, value.length)];
        
        if(numberofMatch >0) {
            return YES;
        }else {
            return NO;
        }
    case 18:
        year = [value substringWithRange:NSMakeRange(6,4)].intValue;
        if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
            
            regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                                                     options:NSRegularExpressionCaseInsensitive
                                                                       error:nil];//测试出生日期的合法性
        }else {
            regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                     options:NSRegularExpressionCaseInsensitive
                                                                       error:nil];//测试出生日期的合法性
        }
        numberofMatch = [regularExpression numberOfMatchesInString:value
                                                           options:NSMatchingReportProgress
                                                             range:NSMakeRange(0, value.length)];
        
        if(numberofMatch >0) {
            int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
            int Y = S %11;
            NSString *M =@"F";
            NSString *JYM =@"10X98765432";
            M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
            if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
                return YES;// 检测ID的校验位
            }else {
                return NO;
            }
            
        }else {
            return NO;
        }
    default:
        return NO;
}

}

/** 银行卡号有效性问题Luhn算法

@end

上一篇 下一篇

猜你喜欢

热点阅读