常用正则表达式

#pragma mark - 身份证号

+ (BOOL) IsIdentityCard:(NSString *)IDCardNumber

{

    if (IDCardNumber.length <= 0) {

        return NO;

    }

    //大陆身份证

//    NSString *regexMainland = @"^(\\d{14}|\\d{17})(\\d|[xX])$";

    NSString *regexMainland = @"(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";

    NSPredicate *idCardPredicate_Mainland = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexMainland];

    BOOL mainLand = [idCardPredicate_Mainland evaluateWithObject:IDCardNumber];


    //香港永久居民身份证

    NSString *regexHongKongPermanent = @"^[A-Z][0-9]{8,12}$";

    NSPredicate *idCardPredicate_HongKongPermanent = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexHongKongPermanent];

    BOOL HongKongPermanent = [idCardPredicate_HongKongPermanent evaluateWithObject:IDCardNumber];


    //香港居民身份证

    NSString *regexHongKong = @"^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$";

    NSPredicate *idCardPredicate_HongKong = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexHongKong];

    BOOL HongKong = [idCardPredicate_HongKong evaluateWithObject:IDCardNumber];


    //澳门身份证

    NSString *regexMacau = @"[157][0-9]{6}\\([0-9]\\)";

    NSPredicate *idCardPredicate_Macau = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexMacau];

    BOOL Macau = [idCardPredicate_Macau evaluateWithObject:IDCardNumber];


    //台湾身份证

    NSString *regexTaiwan = @"^[A-KM-QT-XZ]{1}[0-9]{9}$";

    NSPredicate *idCardPredicate_Taiwan = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexTaiwan];

    BOOL Taiwan = [idCardPredicate_Taiwan evaluateWithObject:IDCardNumber];

    if (mainLand == NO && HongKongPermanent == NO && HongKong == NO && Macau == NO && Taiwan == NO) {

        return NO;

    }else{

        return YES;

    }

}

#pragma mark - 手机号码

+ (BOOL) IsPhoneNumber:(NSString *)number

{

    NSString *phoneRegex1=@"1[34578]([0-9]){9}";

    NSPredicate *phoneTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex1];

    return  [phoneTest1 evaluateWithObject:number];

}

#pragma mark - 邮箱

+ (BOOL) IsEmailAdress:(NSString *)Email

{

    NSString *emailCheck = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailCheck];

    return [emailTest evaluateWithObject:Email];

}

#pragma mark - 银行卡

+ (BOOL) IsBankCard:(NSString *)cardNumber

{

    if(cardNumber.length==0)

    {

        return NO;

    }

    NSString *digitsOnly = @"";

    char c;

    for (int i = 0; i < cardNumber.length; i++)

    {

        c = [cardNumber characterAtIndex:i];

        if (isdigit(c))

        {

            digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];

        }

    }

    int sum = 0;

    int digit = 0;

    int addend = 0;

    BOOL timesTwo = false;

    for (NSInteger i = digitsOnly.length - 1; i >= 0; i--)

    {

        digit = [digitsOnly characterAtIndex:i] - '0';

        if (timesTwo)

        {

            addend = digit * 2;

            if (addend > 9) {

                addend -= 9;

            }

        }

        else {

            addend = digit;

        }

        sum += addend;

        timesTwo = !timesTwo;

    }

    int modulus = sum % 10;

    return modulus == 0;

}

你可能感兴趣的:(常用正则表达式)