/** 科學(xué)計(jì)數(shù)法正則表達(dá)式 */
#define REGEX_SCIEN? ? ? ? ? ? ? ? ? ? ? (@"^((\\d+.?\\d+)[Ee]{1}(\\d+))$")
/** 手機(jī)號(hào)驗(yàn)證正則表達(dá)式 */
NSString *regex_phonenum = @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[358]+\\d{9}|\\d{8}|\\d{7}";
/**手機(jī)號(hào)碼正則2.0**/
+ (BOOL)phoneValiMobile:(NSString *)mobile
{
? ? mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
? if (mobile.length != 11)
? {
? ?return NO;
? ?}else{
/*** 移動(dòng)號(hào)段正則表達(dá)式*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/*** 聯(lián)通號(hào)段正則表達(dá)式*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/*** 電信號(hào)段正則表達(dá)式*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
if (isMatch1 || isMatch2 || isMatch3) {
return YES;
}else{
return NO;
}
}
}
/** 金額格式正則表達(dá)式 */
#define REGEX_AMOUNT? ? ? ? ? ? ? ? ? ? ? (@"^[-]?[\\d]{1,10}([.]{1}[\\d]{1,2})?$")
/** yyyyMM正則表達(dá)式 */
#define REGEX_MONTH? ? ? ? ? ? ? ? ? ? ? (@"^([1-9]\\d{3})(([0][1-9])|([1][0-2]))$")
/** yyyyMMdd日期格式正則表達(dá)式 */
#define REGEX_DATE? ? ? ? ? ? ? ? ? ? ? ? (@"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229)")
//? 年-月-日 :/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/
/** Email驗(yàn)證正則表達(dá)式 */
#define REGEX_EMAIL? ? ? ? ? ? ? ? ? ? ? (@"^(([_\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([_\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?))$")
//匹配網(wǎng)址鏈接
NSString *regex_http = @"(https?|ftp|file)+://[^\\s]*";
匹配特定數(shù)字:
^[1-9]d*$ //匹配正整數(shù)
^-[1-9]d*$ //匹配負(fù)整數(shù)
^-?[1-9]d*$ //匹配整數(shù)
^[1-9]d*|0$ //匹配非負(fù)整數(shù)(正整數(shù) + 0)
^-[1-9]d*|0$ //匹配非正整數(shù)(負(fù)整數(shù) + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ //匹配正浮點(diǎn)數(shù)
^-([1-9]d*.d*|0.d*[1-9]d*)$ //匹配負(fù)浮點(diǎn)數(shù)
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //匹配浮點(diǎn)數(shù)
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //匹配非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ //匹配非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)
評(píng)注:處理大量數(shù)據(jù)時(shí)有用,具體應(yīng)用時(shí)注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26個(gè)英文字母組成的字符串
^[A-Z]+$ //匹配由26個(gè)英文字母的大寫組成的字符串
^[a-z]+$ //匹配由26個(gè)英文字母的小寫組成的字符串
^[A-Za-z0-9]+$ //匹配由數(shù)字和26個(gè)英文字母組成的字符串
^w+$ //匹配由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串
在使用RegularExpressionValidator驗(yàn)證控件時(shí)的驗(yàn)證功能及其驗(yàn)證表達(dá)式介紹如下:
只能輸入數(shù)字:“^[0-9]*$”
只能輸入n位的數(shù)字:“^d{n}$”
只能輸入至少n位數(shù)字:“^d{n,}$”
只能輸入m-n位的數(shù)字:“^d{m,n}$”
只能輸入零和非零開頭的數(shù)字:“^(0|[1-9][0-9]*)$”
只能輸入有兩位小數(shù)的正實(shí)數(shù):“^[0-9]+(.[0-9]{2})?$”
只能輸入有1-3位小數(shù)的正實(shí)數(shù):“^[0-9]+(.[0-9]{1,3})?$”
只能輸入非零的正整數(shù):“^+?[1-9][0-9]*$”
只能輸入非零的負(fù)整數(shù):“^-[1-9][0-9]*$”
只能輸入長(zhǎng)度為3的字符:“^.{3}$”
只能輸入由26個(gè)英文字母組成的字符串:“^[A-Za-z]+$”
只能輸入由26個(gè)大寫英文字母組成的字符串:“^[A-Z]+$”
只能輸入由26個(gè)小寫英文字母組成的字符串:“^[a-z]+$”
只能輸入由數(shù)字和26個(gè)英文字母組成的字符串:“^[A-Za-z0-9]+$”
只能輸入由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串:“^w+$”
驗(yàn)證用戶密碼:“^[a-zA-Z]w{5,17}$”正確格式為:以字母開頭,長(zhǎng)度在6-18之間,
只能包含字符、數(shù)字和下劃線。
驗(yàn)證是否含有^%&'',;=?$"等字符:“[^%&'',;=?$x22]+”
只能輸入漢字:“^[u4e00-u9fa5],{0,}$”
匹配首尾空格的正則表達(dá)式:(^s*)|(s*$)
匹配中文字符的正則表達(dá)式: [u4e00-u9fa5]
//校驗(yàn)登錄名:只能輸入5-20個(gè)以字母開頭、可帶數(shù)字、“_”、“.”的字串:/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/
//校驗(yàn)用戶姓名:只能輸入1-30個(gè)以字母開頭的字串:/^[a-zA-Z]{1,30}$/
//校驗(yàn)密碼:只能輸入6-20個(gè)字母、數(shù)字、下劃線:/^(\w){6,20}$/