IOS常用正規表示式

2022-05-04 19:36:20 字數 3280 閱讀 2711

原文:

匹配中文:[\u4e00-\u9fa5] 

英文本母:[a-za-z] 

數字:[0-9] 

匹配中文,英文本母和數字及_: 

^[\u4e00-\u9fa5_a-za-z0-9]+$

同時判斷輸入長度:

[\u4e00-\u9fa5_a-za-z0-9_]

^[\w\u4e00-\u9fa5\uf900-\ufa2d]*$ 1、乙個正規表示式,只含有漢字、數字、字母、下劃線不能以下劃線開頭和結尾:

^(?!_)(?!.*?_$)[a-za-z0-9_\u4e00-\u9fa5]+$  其中:

^  與字串開始的地方匹配

(?!_)  不能以_開頭

(?!.*?_$)  不能以_結尾

[a-za-z0-9_\u4e00-\u9fa5]+  至少乙個漢字、數字、字母、下劃線

$  與字串結束的地方匹配

放在程式裡前面加@,否則需要\\進行轉義 @"^(?!_)(?!.*?_$)[a-za-z0-9_\u4e00-\u9fa5]+$"

(或者:@"^(?!_)\w*(?34555#5'

[\u4e00-\u9fa50-9a-za-z_]    eiieng_89_   --->   eiieng_89_

_';'eiieng_88&*9_    -->  _';'eiieng_88&*9_

_';'eiieng_88_&*9_  -->  _';'eiieng_88_&*9_

最長不得超過7個漢字,或14個位元組(數字,字母和下劃線)正規表示式

^[\u4e00-\u9fa5]$|^[\da-za-z_]$

/ 再次編輯----------------

匹配月份的正規表示式

^[1-9]$|^1[0-2]$

注:個位數月份匹配方式 前面不能加 0。

^0?[1-9]$|^1[0-2]$

注:個位數月份前可以加0或者不加。

匹配年份19**或者20**

^(19|20)[0-9]$

用法:[objc]view plain

copy

print

?+ (bool)isemailaddress:(nsstring*)candidate  

";  

nspredicate* emailtest = [nspredicatepredicatewithformat:@"self matches %@",emailregex];  

return [emailtestevaluatewithobject:candidate];  

}[objc]view plain

copy

print

?-(nsnumber *)asnumber;  

returnnil;  

}[objc]view plain

copy

print

?//摘自nsstring+beeextension.mm  

- (bool)isusername  

$)";  

nspredicate *   pred = [nspredicatepredicatewithformat:@"self matches %@",regex];  

return [predevaluatewithobject:self];  

}  - (bool)ispassword  

$)";  

nspredicate *   pred = [nspredicatepredicatewithformat:@"self matches %@",regex];  

return [predevaluatewithobject:self];    

}  - (bool)isemail  

";  

nspredicate *   pred = [nspredicatepredicatewithformat:@"self matches %@",regex];  

return [predevaluatewithobject:self];  

}  - (bool)isurl  

- (bool)istelephone  

$";  

nsstring * cm = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d$";  

nsstring * cu = @"^1(3[0-2]|5[256]|8[56])\\d$";  

nsstring * ct = @"^1((33|53|8[09])[0-9]|349)\\d$";  

nsstring * phs = @"^0(10|2[0-5789]|\\d)\\d$";  

nspredicate *regextestmobile = [nspredicatepredicatewithformat:@"self matches %@",mobile];  

nspredicate *regextestcm = [nspredicatepredicatewithformat:@"self matches %@",cm];  

nspredicate *regextestcu = [nspredicatepredicatewithformat:@"self matches %@",cu];  

nspredicate *regextestct = [nspredicatepredicatewithformat:@"self matches %@",ct];  

nspredicate *regextestphs = [nspredicatepredicatewithformat:@"self matches %@",phs];  

return  [regextestmobileevaluatewithobject:self]   ||  

[regextestphsevaluatewithobject:self]      ||  

[regextestctevaluatewithobject:self]       ||  

[regextestcuevaluatewithobject:self]       ||  

[regextestcmevaluatewithobject:self];  

}  揭開正規表示式的神秘面紗

regexlib.com(正規表示式庫查詢)

以上的正則匹配都用到了 nspredicate ,關於 nspredicate 的更多地用法可以看這裡。

IOS常用正規表示式判斷

校驗密碼 bool checkpass nsstring pass nsstring regex a za z0 9 nspredicate predicate nspredicate predicatewithformat self matches and self.length 5 and se...

iOS 正規表示式

正規表示式,又稱正規表示法 常規表示法 英語 regular expression,在 中常簡寫為regex regexp或re 電腦科學的乙個概念。正規表示式使用單個字串來描述 匹配一系列符合某個句法規則的字串。在很多文字編輯器裡,正規表示式通常被用來檢索 替換那些符合某個模式的文字。系統自帶的,...

ios 正規表示式

ab7 必須找到連在一起的ab7 0 9 找到0 9中的乙個即可 0 9 找到0 9中的乙個即可 0123456789 找到0 9中的乙個即可 d表示數字,d表示3個數字連在一起 d d d表示3個數字連在一起 da表示3個數字2個a連在一起 d 2個或4個數字連在一起 表示開始 表示結束 d 開始...