美文网首页
正则表示

正则表示

作者: 会飞的大西瓜v | 来源:发表于2017-03-09 14:11 被阅读23次

    手机号

    • 移动号段正则表达式
    NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
    
    • 联通号段正则表达式
    NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
    
    • 电信号段正则表达式
     NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
    
    • 电话号码以 13 , 15 , 18 开头
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    

    判断是不是 Email

    + (BOOL)validateEmail:(NSString *)email {
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [emailTest evaluateWithObject:email];
    }
    

    判断是不是money

    + (BOOL)validaeMoney:(NSString *)money {
        NSString *phoneRegex = @"^[0-9]+(\\.[0-9]{1,2})?$";
        NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
        return [phoneTest evaluateWithObject:money];
    }
    

    昵称 ,用户名 ,密码 判断

    + (BOOL)validateUsername:(NSString *)username {
        NSString *userNameRegex = @"^[A-Za-z0-9\u4e00-\u9fa5]{4,20}+$";
        NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
        return [userNamePredicate evaluateWithObject:username];
    }
    
    + (BOOL)validateNickname:(NSString *)nickname {
        NSString *nicknameRegex = @"^[A-Za-z0-9\u4e00-\u9fa5_-]{1,20}+$";
        NSPredicate *nicknamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nicknameRegex];
        return [nicknamePredicate evaluateWithObject:nickname];
    }
    
    + (BOOL)validatePassword:(NSString *)password {
        NSString *passwordRegex = @"^[a-zA-Z0-9-]{6,20}+$";
        NSPredicate *passwordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passwordRegex];
        return [passwordPredicate evaluateWithObject:password];
    }
    

    相关文章

      网友评论

          本文标题:正则表示

          本文链接:https://www.haomeiwen.com/subject/phewgttx.html