美文网首页
iOS常用正则表达式

iOS常用正则表达式

作者: 冷月花 | 来源:发表于2018-03-21 09:51 被阅读8次

    #pragma 正则匹配手机号 

    + (BOOL)checkTelNumber:(NSString*) telNumber{
        NSString *pattern = @"^1+[3578]+\\d{9}";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
        BOOLisMatch = [predevaluateWithObject:telNumber];
        returnisMatch;
    }

    #pragma 正则匹配用户密码6-18位数字和字母组合 

    + (BOOL)checkPassword:(NSString *) password {   
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}";   
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];     BOOL isMatch = [pred evaluateWithObject:password];
     return isMatch;     
    }

    #pragma 正则匹配用户姓名,20位的中文或英文

     + (BOOL)checkUserName : (NSString *) userName {   
    NSString *pattern = @"^[a-zA-Z一-龥]{1,20}";   
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];    BOOL isMatch = [pred evaluateWithObject:userName];
    return isMatch;     
    }

    #pragma正则匹配用户身份证号15或18位

    + (BOOL)checkUserIdCard: (NSString*) idCard{
        BOOLflag;
        if(idCard.length<=0) {
        flag =NO;
        returnflag;
    }
        NSString *regex2 =@"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
        NSPredicate*identityCardPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];
        return[identityCardPredicateevaluateWithObject:idCard];
    }

    #pragma正则匹配邮箱

    + (BOOL) checkUserEmail:(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[emailTestevaluateWithObject:email];

    }

    #pragma正则匹配银行卡号

    + (BOOL)checkUserBankNumber:(NSString*)BankNumber{

        NSString *regex2 =@"^\\d{16,19}$|^\\d{6}[- ]\\d{10,13}$|^\\d{4}[- ]\\d{4}[- ]\\d{4}[- ]\\d{4,7}$";

        NSPredicate*identityCardPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];

        return[identityCardPredicateevaluateWithObject:BankNumber];

    }

    #pragma 正则匹配用验证身份证户身份证号15或18位

    + (BOOL) validateIdentityCard: (NSString*)value

    {

        value = [valuestringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if([valuelength] !=18) {

            returnNO;

        }

        NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";

        NSString*leapMmdd =@"0229";

        NSString *year = @"(19|20)[0-9]{2}";

        NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";

        NSString*yearMmdd = [NSStringstringWithFormat:@"%@%@", year, mmdd];

        NSString*leapyearMmdd = [NSStringstringWithFormat:@"%@%@", leapYear, leapMmdd];

        NSString*yyyyMmdd = [NSStringstringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd,@"20000229"];

        NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";

        NSString*regex = [NSStringstringWithFormat:@"%@%@%@", area, yyyyMmdd  ,@"[0-9]{3}[0-9Xx]"];
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

        if(![regexTestevaluateWithObject:value]) {

            returnNO;

        }

    int summary = ([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;

        NSIntegerremainder = summary %11;

        NSString*checkBit =@"";

        NSString*checkString =@"10X98765432";

        checkBit = [checkStringsubstringWithRange:NSMakeRange(remainder,1)];// 判断校验位

        return [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]];

    }

    相关文章

      网友评论

          本文标题:iOS常用正则表达式

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