首先简单介绍一下正则表达式的基本写法及用法
\d 匹配任何一个数字,即[0-9]
\d+表示1个以上的数字
\d*标示0个或多个数字
\s 表示分隔符,包括空格、制表符和换行符
\s* 0个或多个分隔符
\w 表示字母、数字和下划线,这里还包括Unicode字符,不同的语言里有些不同
\w{4} 表示4个非空白字符
. 表示任何一个字符,不包括换行符
.* 表示任何多个字符,当然也不包括换行符了
^([0-9]) 表示数字开头
([0-9])$ 表示数字结尾
([0-9A-Za-z]) 表示数字字母
| 表示或的关系,如\d|\w 表示数字或字母
(.|[\r\n])* 表示任何多个字符,包括换行符,贪婪扫描
(.|[\r\n])*? 表示任何多个字符,包括换行符,懒惰扫描
(?:.|[\\r\\n]) 以(?:开头时的分组信息,表示不读取到缓冲器里,避免rangeAtIndex调用时产生副作用,后面还会遇到这样的(?:写法
@implementationTSRegularExpressionUtil //邮箱 +(BOOL)validateEmail:(NSString*)email { NSString*emailRegex=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate*emailTest=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",emailRegex]; return[emailTestevaluateWithObject: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=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",phoneRegex]; return[phoneTestevaluateWithObject: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=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",carRegex]; NSLog(@"carTestis%@",carTest); return[carTestevaluateWithObject:carNo]; } //车型 +(BOOL)validateCarType:(NSString*)CarType { NSString*CarTypeRegex=@"^[\u4E00-\u9FFF]+$"; NSPredicate*carTest=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",CarTypeRegex]; return[carTestevaluateWithObject:CarType]; } //用户名 +(BOOL)validateUserName:(NSString*)name { NSString*userNameRegex=@"^[A-Za-z0-9]{4,20}+$"; NSPredicate*userNamePredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",userNameRegex]; BOOLB=[userNamePredicateevaluateWithObject:name]; returnB; } //密码 +(BOOL)validatePassword:(NSString*)passWord { NSString*passWordRegex=@"^[a-zA-Z0-9]{6,20}+$"; NSPredicate*passWordPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",passWordRegex]; return[passWordPredicateevaluateWithObject:passWord]; } //昵称 +(BOOL)validateNickname:(NSString*)nickname { NSString*nicknameRegex=@"([\u4e00-\u9fa5]{2,5})(·[\u4e00-\u9fa5]{2,5})*"; NSPredicate*passWordPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",nicknameRegex]; return[passWordPredicateevaluateWithObject:nickname]; } //身份证号 +(BOOL)validateIdentityCard:(NSString*)identityCard { BOOLflag; if(identityCard.length<=0){ flag=NO; returnflag; } NSString*regex2=@"^(\\d{14}|\\d{17})(\\d|[xX])$"; NSPredicate*identityCardPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[identityCardPredicateevaluateWithObject:identityCard]; } //银行卡 +(BOOL)validateBankCardNumber:(NSString*)bankCardNumber { BOOLflag; if(bankCardNumber.length<=0){ flag=NO; returnflag; } NSString*regex2=@"^(\\d{15,30})"; NSPredicate*bankCardPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[bankCardPredicateevaluateWithObject:bankCardNumber]; } //银行卡后四位 +(BOOL)validateBankCardLastNumber:(NSString*)bankCardNumber { BOOLflag; if(bankCardNumber.length!=4){ flag=NO; returnflag; } NSString*regex2=@"^(\\d{4})"; NSPredicate*bankCardPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[bankCardPredicateevaluateWithObject:bankCardNumber]; } //CVN +(BOOL)validateCVNCode:(NSString*)cvnCode { BOOLflag; if(cvnCode.length<=0){ flag=NO; returnflag; } NSString*regex2=@"^(\\d{3})"; NSPredicate*cvnCodePredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[cvnCodePredicateevaluateWithObject:cvnCode]; } //month +(BOOL)validateMonth:(NSString*)month { BOOLflag; if(!month.length==2){ flag=NO; returnflag; } NSString*regex2=@"(^(0)([0-9])$)|(^(1)([0-2])$)"; NSPredicate*monthPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[monthPredicateevaluateWithObject:month]; } //month +(BOOL)validateYear:(NSString*)year { BOOLflag; if(!year.length==2){ flag=NO; returnflag; } NSString*regex2=@"^([1-3])([0-9])$"; NSPredicate*yearPredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[yearPredicateevaluateWithObject:year]; } //verifyCode +(BOOL)validateVerifyCode:(NSString*)verifyCode { BOOLflag; if(!verifyCode.length==6){ flag=NO; returnflag; } NSString*regex2=@"^(\\d{6})"; NSPredicate*verifyCodePredicate=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex2]; return[verifyCodePredicateevaluateWithObject:verifyCode]; } @end原文链接:https://www.f2er.com/regex/361707.html