IOS 常用正则表达式整理(邮箱、身份证号、固化、手机号、纯数字、纯文字等)

前言:常用正则表达式整理,包括邮箱、身份证号、固化、手机号、纯数字、纯文字等

一、对邮箱进行校验

+ (BOOL)checkEmail:(NSString *)email
{
    NSString *emailReg = @"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailReg];
    if ([regextestmobile evaluateWithObject:email] == YES){
        return YES;
    }
    return NO;
}


二、对身份证号进行校验

+ (BOOL)checkPersonID:(NSString *)personID
{
    NSString *person = @"(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",person];
    if ([regextestmobile evaluateWithObject:personID] == YES){
        return YES;
    }
    return NO;
}
三、对固话进行校验

#pragma mark 检验是否是固定电话
+ (BOOL)checkLandlinePhone:(NSString *)landlinePhone
{
    NSString *mobile = @"0\\d{2,3}-\\d{5,9}|0\\d{2,3}-\\d{5,9}";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobile];
    if ([regextestmobile evaluateWithObject:landlinePhone] == YES){
        return YES;
    }
    return NO;
}
四、对手机号进行校验
#pragma mark 检测手机号码是否正确
+ (BOOL)checkMobileNumber:(NSString *)mobileNum{
    mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([mobileNum hasPrefix:@"00"])
        return NO;
    
    if ([mobileNum hasPrefix:@" "]){
        mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    
    /**
     * 手机号码
     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通:130,131,132,152,155,156,185,186
     * 电信:133,1349,153,180,189
     *
     * 新增
     * 移动:152 154 178 183 184
     * 联通:176
     * 电信:177 181
     */
    
    NSString * MOBILE = @"^1(3[0-9]|4[0-9]|5[0-9]|7[67]|8[0-9])\\d{8}$";
    /**
     10         * 中国移动:China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,152,154,157,158,159,178,182,183,184,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[0-247-9]|7[8]|8[2-478])\\d)\\d{7}$";
    /**
     15         * 中国联通:China Unicom
     16         * 130,131,132,152,155,156,176,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|7[6]|8[56])\\d{8}$";
    /**
     20         * 中国电信:China Telecom
     21         * 133,1349,153,177,180,181,189
     22         */
    NSString * CT = @"^1((33|53|7[7]|8[019])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号:010,020,021,022,023,024,025,027,028,029
     27         * 号码:七位或八位
     28         */
    
    NSString *telNum = @"^0\\d[1-9]{1}(\\d[0-9]{1,2}\\d[1-9]{1})\\d{4,6}";
    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];
    NSPredicate *regextestTel = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", telNum];
    if ([regextestTel evaluateWithObject:mobileNum]){
        MyLog(@"pass");
    }
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES)
        || ([regextestTel evaluateWithObject:mobileNum] == YES)){
        return YES;
    }
    return NO;
}

五、纯数字

NSString *reg = @"^\\d*$";
六、纯字母

NSString *reg = @"^[A-Za-z]*$";
七、首字母为字母,其它为A-Za-z_0-9
NSString *reg = @"^[a-zA-Z]\\w{1,}$";





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值