一、简单的正则规则
1.由数字、26个英文字母或者下划线组成的字符串:
^[0-9a-zA-Z_]{1,}$
2.非负整数(正整数 + 0 ):
^/d+$
3. 正整数:
^[0-9]*[1-9][0-9]*$
4.非正整数(负整数 + 0):
^((-/d+)|(0+))$
5. 负整数 :
^-[0-9]*[1-9][0-9]*$
6.整数:
^-?/d+$
7.非负浮点数(正浮点数 + 0):
^/d+(/./d+)?$
8.正浮点数 :
^(([0-9]+/.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*/.[0-9]+)|([0-9]*[1-9][0-9]*))$
9. 非正浮点数(负浮点数 + 0):
^((-/d+(/./d+)?)|(0+(/.0+)?))$
10.负浮点数 :
^(-(([0-9]+/.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*/.[0-9]+)|([0-9]*[1-9][0-9]*)))$
11. 浮点数 :
^(-?/d+)(/./d+)?$
12.由26个英文字母组成的字符串 :
^[A-Za-z]+$
13. 由26个英文字母的大写组成的字符串 :
^[A-Z]+$
14.由26个英文字母的小写组成的字符串 :
^[a-z]+$
15. 由数字和26个英文字母组成的字符串 :
^[A-Za-z0-9]+$
16.由数字、26个英文字母或者下划线组成的字符串 :
^/w+$
17.email地址 :
^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$
18.url:
^[a-zA-z]+://(/w+(-/w+)*)(/.(/w+(-/w+)*))*(/?/S*)?$
19. 年-月-日:
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/
20.月/日/年:
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/
21.Emil:
^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$
22. 电话号码:
(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?
23.IP地址:
^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$
24. 匹配中文字符的正则表达式:
[/u4e00-/u9fa5]
25.匹配双字节字符(包括汉字在内):
[^/x00-/xff]
26. 匹配空行的正则表达式:
/n[/s| ]*/r
27.匹配HTML标记的正则表达式:
/<(.*)>.*<///1>|<(.*) //>/
28.匹配首尾空格的正则表达式:
(^/s*)|(/s*$)
29.匹配Email地址的正则表达式:
/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*
30. 匹配网址URL的正则表达式:
^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$
31. 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
32. 匹配国内电话号码:
(/d{3}-|/d{4}-)?(/d{8}|/d{7})?
33.匹配腾讯QQ号:
^[1-9]*[1-9][0-9]*$
34. 只能输入数字:
^[0-9]*$
35.只能输入n位的数字:
^/d{n}$
36.只能输入至少n位的数字:
^/d{n,}$
37.只能输入m~n位的数字:
^/d{m,n}$
38.只能输入零和非零开头的数字:
^(0|[1-9][0-9]*)$
39.只能输入有两位小数的正实数:
^[0-9]+(.[0-9]{2})?$
40. 只能输入有1~3位小数的正实数:
^[0-9]+(.[0-9]{1,3})?$
41.只能输入非零的正整数:
^/+?[1-9][0-9]*$
42. 只能输入非零的负整数:
^/-[1-9][0-9]*$
43.只能输入长度为3的字符:
^.{3}$
44. 只能输入由26个英文字母组成的字符串:
^[A-Za-z]+$
45.只能输入由26个大写英文字母组成的字符串:
^[A-Z]+$
46. 只能输入由26个小写英文字母组成的字符串:
^[a-z]+$
47.只能输入由数字和26个英文字母组成的字符串:
^[A-Za-z0-9]+$
48. 只能输入由数字和26个英文字母或者下划线组成的字符串:
^/w+$
49.验证用户密码(正确格式为: 以字母开头,长度在5~17 之间,只能包含字符、数字和下划线)
^[a-zA-Z]/w{5,17}$
50.验证是否包含有 ^%&',;=?$/"等字符:
[^%&',;=?$/x22]+
51.只能输入汉字:
^[u4e00-u9fa5]{0,}$
52、只含有汉字、数字、字母、下划线不能以下划线开头和结尾
^(?!_)(?!.*?_$)[a-zA-Z0-9_u4e00-u9fa5]+$
53、只含有汉字、数字、字母、下划线,下划线位置不限
^[a-zA-Z0-9_u4e00-u9fa5]+$
54、2~4个汉字
^[u4E00-u9FA5]{2,4}$
二、列举一些简答的demo如下:
//邮箱 + (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})(·[\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]; }