正則表達(dá)式的簡單使用

直接上代碼 這里面包含很多的正則匹配

.h


/**
 *  驗證字符串是否為空值
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsEmpty:(NSString *)targetString;

/**
 *  驗證字符串是否為非負(fù)整數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonnegativeInteger:(NSString *)targetString;

/**
 *  驗證字符串是否為正整數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsPositiveInteger:(NSString *)targetString;

/**
 *  驗證字符串是否為非正整數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonPositiveInteger:(NSString *)targetString;

/**
 *  驗證字符串是否為負(fù)整數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNegativeInteger:(NSString *)targetString;

/**
 *  驗證字符串是否為整數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsInteger:(NSString *)targetString;

/**
 *  驗證字符串是否為非負(fù)浮點數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonNegativeFloatingPoint:(NSString *)targetString;

/**
 *  驗證字符串是否為正浮點數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsPositiveFloatingPoint:(NSString *)targetString;

/**
 *  驗證字符串是否為非正浮點數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNonPositiveFloatingPoint:(NSString *)targetString;

/**
 *  驗證字符串是否為負(fù)浮點數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNegativeFloatingPoint:(NSString *)targetString;

/**
 *  驗證字符串是否為浮點數(shù)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsFloatingPoint:(NSString *)targetString;

/**
 *  驗證字符串是否為由26個英文字母組成的字符串
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIs26EnglishLetter:(NSString *)targetString;

/**
 *  驗證字符串是否為由26個英文字母的大寫組成的字符串
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIs26CapitalEnglishLetter:(NSString *)targetString;

/**
 *  驗證字符串是否為由26個英文字母的小寫組成的字符串
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIs26LowercaseEnglishLetter:(NSString *)targetString;

/**
 *  驗證字符串是否為由數(shù)字和26個英文字母組成的字符串
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNumbersAnd26EnglishLetter:(NSString *)targetString;

/**
 *  驗證字符串是否為由數(shù)字、26個英文字母或者下劃線組成的字符串
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsNumbersAnd26EnglishLetterAndBottomLine:(NSString *)targetString;

/**
 *  驗證字符串是否為合法的郵箱email
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidMailbox:(NSString *)targetString;

/**
 *  驗證字符串是否為合法的電話號碼
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidPhoneNumber:(NSString *)targetString;

/**
 *  驗證字符串是否為合法的IP地址
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidIPAdress:(NSString *)targetString;

/**
 *  驗證字符串是否為中文
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsChineseText:(NSString *)targetString;

/**
 *  驗證字符串是否為雙字節(jié)字符(包括漢字在內(nèi))
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDoubleByteCharacter:(NSString *)targetString;

/**
 *  驗證字符串是否為空行
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsBlankLine:(NSString *)targetString;

/**
 *  驗證字符串是合法的url
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidURL:(NSString *)targetString;

/**
 *  驗證字符串是符合時間格式:年-月-日
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDate_YearMonthDay:(NSString *)targetString;

/**
 *  驗證字符串是符合時間格式:月/日/年
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDate_MonthDayYear:(NSString *)targetString;

/**
 *  驗證字符串是否為HTML標(biāo)記
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsHTML:(NSString *)targetString;

/**
 *  驗證帳號是否合法(字母開頭戚宦,允許5-16字節(jié)杀狡,允許字母數(shù)字下劃線)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsLegalUserName:(NSString *)targetString;

/**
 *  驗證用戶密碼(正確格式為: 以字母開頭,長度在5~17 之間胁附,只能包含字符、數(shù)字和下劃線)
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsLegalPassword:(NSString *)targetString;


/**
 *  驗證是否為國內(nèi)電話號碼
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsDomesticTelephoneNumber:(NSString *)targetString;

/**
 *  驗證QQ號
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsValidQQ:(NSString *)targetString;

/**
 *  驗證是否只是數(shù)字
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyNumber:(NSString *)targetString;

/**
 *  驗證只能輸入n位的數(shù)字
 *
 *  @param targetString 目標(biāo)字符串
 *  @param figure       位數(shù)
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyManyNumber:(NSString *)targetString withFigure:(NSInteger)figure;

/**
 *  驗證只能輸入至少n位的數(shù)字
 *
 *  @param targetString 目標(biāo)字符串
 *  @param figure       位數(shù)
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyLeastManyNumber:(NSString *)targetString withFigure:(NSInteger)figure;

/**
 *  驗證只能輸入m~n位的數(shù)字
 *
 *  @param targetString 目標(biāo)字符串
 *  @param fromFigure   m位
 *  @param toFigure     n位
 *  @return YES/NO
 */
-(BOOL)validateIsOnlyNumber:(NSString *)targetString fromFigure:(NSInteger)fromFigure toFigure:(NSInteger)toFigure;

/**
 *  驗證是否包含有 ^%&',;=?$/"等字符
 *
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
-(BOOL)validateIsSpecialCharacters:(NSString *)targetString;

/**
 *  實例方法自定義正則驗證
 *
 *  @param customRegex  自定義的正則表達(dá)式
 *  @param targetString 目標(biāo)字符串
 *
 *  @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  自定義的正則表達(dá)式
 *  @param targetString 目標(biāo)字符串
 *
 *  @return YES/NO
 */
+ (BOOL)validateCustomRegex:(NSString *)customRegex TargetString:(NSString *)targetString;

@end

.m


//最終正則匹配結(jié)果
-(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;
}

//驗證字符串是否為非負(fù)整數(shù)
-(BOOL)validateIsNonnegativeInteger:(NSString *)targetString{
    
    NSString *regex = @"^\\d+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

// 驗證字符串是否為正整數(shù)
-(BOOL)validateIsPositiveInteger:(NSString *)targetString{
    
    NSString *regex = @"^[0-9]*[1-9][0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為非正整數(shù)
-(BOOL)validateIsNonPositiveInteger:(NSString *)targetString{
    
    NSString *regex = @"^((-\\d+)|(0+))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為負(fù)整數(shù)
-(BOOL)validateIsNegativeInteger:(NSString *)targetString{
    
    NSString *regex = @"^-[0-9]*[1-9][0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為整數(shù)
-(BOOL)validateIsInteger:(NSString *)targetString{
    
    NSString *regex = @"^-?\\d+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為非負(fù)浮點數(shù)
-(BOOL)validateIsNonNegativeFloatingPoint:(NSString *)targetString{
    
    NSString *regex = @"^\\d+(\\.\\d+)?$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為正浮點數(shù)
-(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];
}

//驗證字符串是否為非正浮點數(shù)
-(BOOL)validateIsNonPositiveFloatingPoint:(NSString *)targetString{
    
    NSString *regex = @"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為負(fù)浮點數(shù)
-(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];
}

//驗證字符串是否為浮點數(shù)
-(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];
}

//驗證字符串是否為由數(shù)字和26個英文字母組成的字符串
-(BOOL)validateIsNumbersAnd26EnglishLetter:(NSString *)targetString{
    
    NSString *regex = @"^[A-Za-z0-9]+$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證字符串是否為由數(shù)字滓彰、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];
}

//驗證字符串是否為雙字節(jié)字符(包括漢字在內(nèi))
-(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標(biāo)記
-(BOOL)validateIsHTML:(NSString *)targetString{
    
    NSString *regex = @"^(\\<(.*)>.*<\\\1>|<(.*) \\>\\)$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}


//驗證帳號是否合法(字母開頭控妻,允許5-16字節(jié),允許字母數(shù)字下劃線)
-(BOOL)validateIsLegalUserName:(NSString *)targetString{
    
    NSString *regex = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證用戶密碼(正確格式為: 以字母開頭揭绑,長度在5~17 之間弓候,只能包含字符、數(shù)字和下劃線)
-(BOOL)validateIsLegalPassword:(NSString *)targetString{
    
    NSString *regex = @"^[a-zA-Z]\\w{5,17}$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

//驗證是否為國內(nèi)電話號碼
-(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];
}

//驗證是否只是數(shù)字
-(BOOL)validateIsOnlyNumber:(NSString *)targetString{
    
    NSString *regex = @"^[0-9]*$";
    return [self regexPatternResultWithRegex:regex TargetString:targetString];
}

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

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

//驗證只能輸入m~n位的數(shù)字
-(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 數(shù)字字符
    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(@"我是數(shù)字--類方法-->%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(@"我是數(shù)字--實例方法-->%d",[tool validateIsValidQQ:@"1383478"]);
    NSLog(@"我是郵箱--實例方法-->%d",[tool validateIsValidMailbox:@"163@qq.com"]);
    NSLog(@"我是漢字中文--實例方法-->%d",[tool validateCustomRegex:@"^[\u4E00-\u9FA5]*$" TargetString:@"我只是中文"]);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末诚纸,一起剝皮案震驚了整個濱河市撰筷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌畦徘,老刑警劉巖毕籽,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異井辆,居然都是意外死亡关筒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門杯缺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒸播,“玉大人,你說我怎么就攤上這事萍肆∨塾埽” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵塘揣,是天一觀的道長包雀。 經(jīng)常有香客問我,道長亲铡,這世上最難降的妖魔是什么才写? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任葡兑,我火速辦了婚禮,結(jié)果婚禮上赞草,老公的妹妹穿的比我還像新娘讹堤。我一直安慰自己,他們只是感情好厨疙,可當(dāng)我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布洲守。 她就那樣靜靜地躺著,像睡著了一般沾凄。 火紅的嫁衣襯著肌膚如雪岖沛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天搭独,我揣著相機與錄音,去河邊找鬼廊镜。 笑死牙肝,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的嗤朴。 我是一名探鬼主播配椭,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼雹姊!你這毒婦竟也來了股缸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤吱雏,失蹤者是張志新(化名)和其女友劉穎敦姻,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體歧杏,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡镰惦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了犬绒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旺入。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖凯力,靈堂內(nèi)的尸體忽然破棺而出茵瘾,到底是詐尸還是另有隱情,我是刑警寧澤咐鹤,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布拗秘,位于F島的核電站,受9級特大地震影響慷暂,放射性物質(zhì)發(fā)生泄漏聘殖。R本人自食惡果不足惜晨雳,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奸腺。 院中可真熱鬧餐禁,春花似錦、人聲如沸突照。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽讹蘑。三九已至末盔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間座慰,已是汗流浹背陨舱。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留版仔,地道東北人游盲。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像蛮粮,于是被迫代替她去往敵國和親益缎。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容

  • iOS -正則表達(dá)式的簡單使用 ?建議收藏然想,用到時候一查就明白了 下面兩個方法我用的也不是很熟練莺奔,如果有懂得朋友歡...
    xx_cc閱讀 8,558評論 17 177
  • 正則表達(dá)式到底是什么東西?字符是計算機軟件處理文字時最基本的單位变泄,可能是字母令哟,數(shù)字,標(biāo)點符號杖刷,空格励饵,換行符,漢字等...
    獅子挽歌閱讀 2,136評論 0 9
  • 初衷:看了很多視頻滑燃、文章役听,最后卻通通忘記了,別人的知識依舊是別人的表窘,自己卻什么都沒獲得典予。此系列文章旨在加深自己的印...
    DCbryant閱讀 3,982評論 0 20
  • 注:本篇文章只為方便查看,特此保留乐严,如有冒犯瘤袖,敬請諒解!0貉椤捂敌! 本文目標(biāo) 30分鐘內(nèi)讓你明白正則表達(dá)式是什么艾扮,并對它...
    阿杰Alex閱讀 1,478評論 0 10
  • 1逆济、圣誕老人的由來 圣誕老人原來的名字叫做尼古拉酌予,在第四世紀(jì)的時候,出生在小亞細(xì)亞巴大拉城奖慌,家庭富有抛虫,父母親是非常...
    F公子閱讀 443評論 0 1