正则表达式的简单使用

直接上代码 这里面包含很多的正则匹配

.h


/**
 *  验证字符串是否为空值
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsEmpty:(NSString *)targetString;

/**
 *  验证字符串是否为非负整数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonnegativeInteger:(NSString *)targetString;

/**
 *  验证字符串是否为正整数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsPositiveInteger:(NSString *)targetString;

/**
 *  验证字符串是否为非正整数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonPositiveInteger:(NSString *)targetString;

/**
 *  验证字符串是否为负整数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNegativeInteger:(NSString *)targetString;

/**
 *  验证字符串是否为整数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsInteger:(NSString *)targetString;

/**
 *  验证字符串是否为非负浮点数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonNegativeFloatingPoint:(NSString *)targetString;

/**
 *  验证字符串是否为正浮点数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsPositiveFloatingPoint:(NSString *)targetString;

/**
 *  验证字符串是否为非正浮点数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonPositiveFloatingPoint:(NSString *)targetString;

/**
 *  验证字符串是否为负浮点数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNegativeFloatingPoint:(NSString *)targetString;

/**
 *  验证字符串是否为浮点数
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsFloatingPoint:(NSString *)targetString;

/**
 *  验证字符串是否为由26个英文字母组成的字符串
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIs26EnglishLetter:(NSString *)targetString;

/**
 *  验证字符串是否为由26个英文字母的大写组成的字符串
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIs26CapitalEnglishLetter:(NSString *)targetString;

/**
 *  验证字符串是否为由26个英文字母的小写组成的字符串
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIs26LowercaseEnglishLetter:(NSString *)targetString;

/**
 *  验证字符串是否为由数字和26个英文字母组成的字符串
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNumbersAnd26EnglishLetter:(NSString *)targetString;

/**
 *  验证字符串是否为由数字、26个英文字母或者下划线组成的字符串
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNumbersAnd26EnglishLetterAndBottomLine:(NSString *)targetString;

/**
 *  验证字符串是否为合法的邮箱email
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidMailbox:(NSString *)targetString;

/**
 *  验证字符串是否为合法的电话号码
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidPhoneNumber:(NSString *)targetString;

/**
 *  验证字符串是否为合法的IP地址
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidIPAdress:(NSString *)targetString;

/**
 *  验证字符串是否为中文
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsChineseText:(NSString *)targetString;

/**
 *  验证字符串是否为双字节字符(包括汉字在内)
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDoubleByteCharacter:(NSString *)targetString;

/**
 *  验证字符串是否为空行
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsBlankLine:(NSString *)targetString;

/**
 *  验证字符串是合法的url
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidURL:(NSString *)targetString;

/**
 *  验证字符串是符合时间格式:年-月-日
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDate_YearMonthDay:(NSString *)targetString;

/**
 *  验证字符串是符合时间格式:月/日/年
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDate_MonthDayYear:(NSString *)targetString;

/**
 *  验证字符串是否为HTML标记
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsHTML:(NSString *)targetString;

/**
 *  验证帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsLegalUserName:(NSString *)targetString;

/**
 *  验证用户密码(正确格式为: 以字母开头,长度在5~17 之间,只能包含字符、数字和下划线)
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsLegalPassword:(NSString *)targetString;


/**
 *  验证是否为国内电话号码
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDomesticTelephoneNumber:(NSString *)targetString;

/**
 *  验证QQ号
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidQQ:(NSString *)targetString;

/**
 *  验证是否只是数字
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyNumber:(NSString *)targetString;

/**
 *  验证只能输入n位的数字
 *
 *  @param targetString 目标字符串
 *  @param figure       位数
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyManyNumber:(NSString *)targetString withFigure:(NSInteger)figure;

/**
 *  验证只能输入至少n位的数字
 *
 *  @param targetString 目标字符串
 *  @param figure       位数
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyLeastManyNumber:(NSString *)targetString withFigure:(NSInteger)figure;

/**
 *  验证只能输入m~n位的数字
 *
 *  @param targetString 目标字符串
 *  @param fromFigure   m位
 *  @param toFigure     n位
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyNumber:(NSString *)targetString fromFigure:(NSInteger)fromFigure toFigure:(NSInteger)toFigure;

/**
 *  验证是否包含有 ^%&',;=?$/"等字符
 *
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsSpecialCharacters:(NSString *)targetString;

/**
 *  实例方法自定义正则验证
 *
 *  @param customRegex  自定义的正则表达式
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
- (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString;




//====================================类方法========================================//
/**
 *  验证邮箱
 *
 *  @param email 邮箱
 *
 *  @return YES/NO
 */
+ (BOOL)validateEmail:(NSString *)email;

/**
 *  验证手机号
 *
 *  @param mobile 手机号
 *
 *  @return YES/NO
 */
+ (BOOL)validateMobile:(NSString *)mobile;

/**
 *  验证车牌号
 *
 *  @param carNo 车牌号
 *
 *  @return YES/NO
 */
+ (BOOL)validateCarNo:(NSString *)carNo;

/**
 *  验证车型
 *
 *  @param CarType 车型
 *
 *  @return YES/NO
 */
+ (BOOL)validateCarType:(NSString *)CarType;

/**
 *  验证用户名
 *
 *  @param name 用户名
 *
 *  @return YES/NO
 */
+ (BOOL)validateUserName:(NSString *)name;

/**
 *  验证密码
 *
 *  @param passWord 密码
 *
 *  @return YES/NO
 */
+ (BOOL)validatePassword:(NSString *)passWord;

/**
 *  验证昵称
 *
 *  @param nickname 昵称
 *
 *  @return YES/NO
 */
+ (BOOL)validateNickname:(NSString *)nickname;

/**
 *  验证身份证
 *
 *  @param identityCard 身份证
 *
 *  @return YES/NO
 */
+ (BOOL)validateIdentityCard:(NSString *)identityCard;

/**
 *  验证银行卡号
 *
 *  @param bankCardNumber 银行卡号
 *
 *  @return YES/NO
 */
+ (BOOL)validateBankCardNumber:(NSString *)bankCardNumber;

/**
 *  验证银行卡后四位
 *
 *  @param bankCardNumber 银行卡后四位
 *
 *  @return YES/NO
 */
+ (BOOL)validateBankCardLastNumber:(NSString *)bankCardNumber;

/**
 *  验证CVN
 *
 *  @param cvnCode CVN
 *
 *  @return YES/NO
 */
+ (BOOL)validateCVNCode:(NSString *)cvnCode;

/**
 *  验证月份
 *
 *  @param month 月份
 *
 *  @return YES/NO
 */
+ (BOOL)validateMonth:(NSString *)month;

/**
 *  验证年
 *
 *  @param year 年
 *
 *  @return YES/NO
 */
+ (BOOL)validateYear:(NSString *)year;

/**
 *  验证验证码
 *
 *  @param verifyCode 验证码
 *
 *  @return YES/NO
 */
+ (BOOL)validateVerifyCode:(NSString *)verifyCode;

/**
 *  类方法自定义正则验证
 *
 *  @param customRegex  自定义的正则表达式
 *  @param targetString 目标字符串
 *
 *  @return YES/NO
 */
+ (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString;

@end

.m


//最终正则匹配结果
-(BOOL)regexPatternResultWithRegex:(NSString *)regex TargetString:(NSString *)targetString{
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:targetString];
}

//验证字符串是否为空值
-(BOOL)validateIsEmpty:(NSString *)targetString{
    
    return targetString.length==0?YES:NO;
}

//验证字符串是否为非负整数
-(BOOL)validateIsNonnegativeInteger:(NSString *)targetString{
    
    NSString *regex = @"^\\d+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

// 验证字符串是否为正整数
-(BOOL)validateIsPositiveInteger:(NSString *)targetString{
    
    NSString *regex = @"^[0-9]*[1-9][0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为非正整数
-(BOOL)validateIsNonPositiveInteger:(NSString *)targetString{
    
    NSString *regex = @"^((-\\d+)|(0+))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为负整数
-(BOOL)validateIsNegativeInteger:(NSString *)targetString{
    
    NSString *regex = @"^-[0-9]*[1-9][0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为整数
-(BOOL)validateIsInteger:(NSString *)targetString{
    
    NSString *regex = @"^-?\\d+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为非负浮点数
-(BOOL)validateIsNonNegativeFloatingPoint:(NSString *)targetString{
    
    NSString *regex = @"^\\d+(\\.\\d+)?$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为正浮点数
-(BOOL)validateIsPositiveFloatingPoint:(NSString *)targetString{
    
    NSString *regex = @"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为非正浮点数
-(BOOL)validateIsNonPositiveFloatingPoint:(NSString *)targetString{
    
    NSString *regex = @"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为负浮点数
-(BOOL)validateIsNegativeFloatingPoint:(NSString *)targetString;{
    
    NSString *regex = @"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为浮点数
-(BOOL)validateIsFloatingPoint:(NSString *)targetString{
    
    NSString *regex = @"^(-?\\d+)(\\.\\d+)?$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为由26个英文字母组成的字符串
-(BOOL)validateIs26EnglishLetter:(NSString *)targetString{
    
    NSString *regex = @"^[A-Za-z]+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为由26个英文字母的大写组成的字符串
-(BOOL)validateIs26CapitalEnglishLetter:(NSString *)targetString{
    
    NSString *regex = @"^[A-Z]+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为由26个英文字母的小写组成的字符串
-(BOOL)validateIs26LowercaseEnglishLetter:(NSString *)targetString{
    
    NSString *regex = @"^[a-z]+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为由数字和26个英文字母组成的字符串
-(BOOL)validateIsNumbersAnd26EnglishLetter:(NSString *)targetString{
    
    NSString *regex = @"^[A-Za-z0-9]+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为由数字、26个英文字母或者下划线组成的字符串
-(BOOL)validateIsNumbersAnd26EnglishLetterAndBottomLine:(NSString *)targetString{
    
    NSString *regex = @"^[0-9a-zA-Z_]{1,}$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为有效的邮箱email
-(BOOL)validateIsValidMailbox:(NSString *)targetString{
    
    //NSString *regex = @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为有效的电话号码
-(BOOL)validateIsValidPhoneNumber:(NSString *)targetString{
    
    NSString *regex = @"^(\\d+-)?(\\d{4}-?\\d{7}|\\d{3}-?\\d{8}|^\\d{7,8})(-\\d+)?$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为有效的ip地址
-(BOOL)validateIsValidIPAdress:(NSString *)targetString{
    
    NSString *regex = @"^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为中文
-(BOOL)validateIsChineseText:(NSString *)targetString{
    
    NSString *regex = @"^[\u4E00-\u9FA5]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为双字节字符(包括汉字在内)
-(BOOL)validateIsDoubleByteCharacter:(NSString *)targetString{
    
    NSString *regex = @"^[\\x00-\\xff]$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为空行
-(BOOL)validateIsBlankLine:(NSString *)targetString{
    
    NSString *regex = @"^(\\n[\\s| ]*\\r)$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是合法的url
-(BOOL)validateIsValidURL:(NSString *)targetString{
    
    NSString *regex = @"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是符合时间格式:年-月-日
-(BOOL)validateIsDate_YearMonthDay:(NSString *)targetString{
    
    NSString *regex = @"^(\\d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是符合时间格式:月/日/年
-(BOOL)validateIsDate_MonthDayYear:(NSString *)targetString{
    
    NSString *regex = @"^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))\\(\\d{2}|\\d{4})$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证字符串是否为HTML标记
-(BOOL)validateIsHTML:(NSString *)targetString{
    
    NSString *regex = @"^(\\<(.*)>.*<\\\1>|<(.*) \\>\\)$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}


//验证帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
-(BOOL)validateIsLegalUserName:(NSString *)targetString{
    
    NSString *regex = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证用户密码(正确格式为: 以字母开头,长度在5~17 之间,只能包含字符、数字和下划线)
-(BOOL)validateIsLegalPassword:(NSString *)targetString{
    
    NSString *regex = @"^[a-zA-Z]\\w{5,17}$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证是否为国内电话号码
-(BOOL)validateIsDomesticTelephoneNumber:(NSString *)targetString{
    
    NSString *regex = @"^((\\d{3}-|\\d{4}-)?(\\d{8}|\\d{7})?)$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证QQ号
-(BOOL)validateIsValidQQ:(NSString *)targetString{
    
    NSString *regex = @"^[1-9]*[1-9][0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证是否只是数字
-(BOOL)validateIsOnlyNumber:(NSString *)targetString{
    
    NSString *regex = @"^[0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证只能输入n位的数字
-(BOOL)validateIsOnlyManyNumber:(NSString *)targetString withFigure:(NSInteger)figure{
    
    NSString *regex = [NSString stringWithFormat:@"^\\d{%ld}$",figure];
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证只能输入至少n位的数字
-(BOOL)validateIsOnlyLeastManyNumber:(NSString *)targetString withFigure:(NSInteger)figure{
    
    NSString *regex = [NSString stringWithFormat:@"^\\d{%ld,}$",figure];
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证只能输入m~n位的数字
-(BOOL)validateIsOnlyNumber:(NSString *)targetString fromFigure:(NSInteger)fromFigure toFigure:(NSInteger)toFigure{
    
    NSString *regex = [NSString stringWithFormat:@"^\\d{%ld,%ld}$",fromFigure,toFigure];
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//验证是否包含有 ^%&',;=?$/"等字符
-(BOOL)validateIsSpecialCharacters:(NSString *)targetString{
    
    NSString *regex = @"^([~!/@#$%^&*()-_=+\\|[{}];:\'\",<.>/?]+)$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//实例方法自定义正则验证
- (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString{
    
    return [self regexPatternResultWithRegex:customRegex TargetString:targetString];
}


//====================================类方法========================================//

//邮箱
+ (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];
}

//手机号码验证
+ (BOOL)validateMobile:(NSString *)mobile
{
    //手机号以13, 15,18开头,八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

//车牌号验证
+ (BOOL)validateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}

//车型
+ (BOOL)validateCarType:(NSString *)CarType
{
    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
    return [carTest evaluateWithObject:CarType];
}

//用户名
+ (BOOL)validateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{4,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}

//密码
+ (BOOL)validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

//昵称
+ (BOOL)validateNickname:(NSString *)nickname
{
    NSString *nicknameRegex = @"([\u4e00-\u9fa5]{2,5})(&middot;[\u4e00-\u9fa5]{2,5})*";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}

//身份证号
+ (BOOL)validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

//银行卡
+ (BOOL)validateBankCardNumber: (NSString *)bankCardNumber
{
    BOOL flag;
    if (bankCardNumber.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{15,30})";
    NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [bankCardPredicate evaluateWithObject:bankCardNumber];
}

//银行卡后四位
+ (BOOL)validateBankCardLastNumber: (NSString *)bankCardNumber
{
    BOOL flag;
    if (bankCardNumber.length != 4) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{4})";
    NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [bankCardPredicate evaluateWithObject:bankCardNumber];
}

//CVN
+ (BOOL)validateCVNCode: (NSString *)cvnCode
{
    BOOL flag;
    if (cvnCode.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{3})";
    NSPredicate *cvnCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [cvnCodePredicate evaluateWithObject:cvnCode];
}

//month
+ (BOOL)validateMonth: (NSString *)month
{
    BOOL flag;
    if (!(month.length == 2)) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"(^(0)([0-9])$)|(^(1)([0-2])$)";
    NSPredicate *monthPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [monthPredicate evaluateWithObject:month];
}

//year
+ (BOOL) validateYear: (NSString *)year
{
    BOOL flag;
    if (!(year.length == 2)) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^([1-3])([0-9])$";
    NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [yearPredicate evaluateWithObject:year];
}

//verifyCode
+ (BOOL) validateVerifyCode: (NSString *)verifyCode
{
    BOOL flag;
    if (!(verifyCode.length == 6)) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{6})";
    NSPredicate *verifyCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [verifyCodePredicate evaluateWithObject:verifyCode];
}

// 类方法自定义正则验证
+ (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString{
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",customRegex];
    return [predicate evaluateWithObject:targetString];
}

@end

ViewController

 //1.类方法
    NSLog(@"我是数字--类方法-->%d",[SHRegexPatternTool validateBankCardNumber:@"6214830115447426"]);
    NSLog(@"我是邮箱--类方法-->%d",[SHRegexPatternTool validateEmail:@"163@qq.com"]);
    NSLog(@"我是汉字中文--类方法-->%d",[SHRegexPatternTool validateCustomRegex:@"^[\u4E00-\u9FA5]*$" TargetString:@"我只是中文"]);
    
    
    //2.实例方法
    SHRegexPatternTool *tool = [[SHRegexPatternTool alloc] init];
    NSLog(@"我是数字--实例方法-->%d",[tool validateIsValidQQ:@"1383478"]);
    NSLog(@"我是邮箱--实例方法-->%d",[tool validateIsValidMailbox:@"163@qq.com"]);
    NSLog(@"我是汉字中文--实例方法-->%d",[tool validateCustomRegex:@"^[\u4E00-\u9FA5]*$" TargetString:@"我只是中文"]);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • iOS -正则表达式的简单使用 ✨建议收藏,用到时候一查就明白了 下面两个方法我用的也不是很熟练,如果有懂得朋友欢...
    xx_cc阅读 8,558评论 17 177
  • 正则表达式到底是什么东西?字符是计算机软件处理文字时最基本的单位,可能是字母,数字,标点符号,空格,换行符,汉字等...
    狮子挽歌阅读 2,134评论 0 9
  • 初衷:看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印...
    DCbryant阅读 3,981评论 0 20
  • 注:本篇文章只为方便查看,特此保留,如有冒犯,敬请谅解!!! 本文目标 30分钟内让你明白正则表达式是什么,并对它...
    阿杰Alex阅读 1,475评论 0 10
  • 1、圣诞老人的由来 圣诞老人原来的名字叫做尼古拉,在第四世纪的时候,出生在小亚细亚巴大拉城,家庭富有,父母亲是非常...
    F公子阅读 441评论 0 1