iOS經典講解之常用的正規表示式方法

2021-07-09 06:24:24 字數 1259 閱讀 5538

1.利用nspredicate(謂詞)匹配

nsstring *email = @「[email protected]」;

nsstring

*regex = @"[a-z0-9a-z._%+-]+@[a-za-z0-9.-]+\\.[a-za-z]";

nspredicate *predicate = [nspredicate

predicatewithformat:@"self matches %@", regex];

bool

isvalid = [predicate 

evaluatewithobject

:email];

謂詞匹配比較靈活,但是需要有謂詞的相關知識。

2.利用rangeofstring:option:直接查詢

nsstring *searchtext = @"// do any additional setup after loading the view, typically from a nib.";

nsrange range = [searchtext rangeofstring:@"(?:[^,])*\\." options:nsregularexpressionsearch];

if (range.location != nsnotfound)

options中設定nsregularexpressionsearch就是表示利用正規表示式匹配,會返回第乙個匹配結果的位置。

3.使用正規表示式類

nsstring *searchtext = @"// do any additional setup after loading the view, typically from a nib.";    

nserror *error = null;

nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@"(?:[^,])*\\." options:nsregularexpressioncaseinsensitive error:&error];

nstextcheckingresult *result = [regex firstmatchinstring:searchtext options:0 range:nsmakerange(0, [searchtext length])];

if (result)

使用系統的正規表示式類(nsregularexpression)會返回匹配的多個結果。

iOS經典講解之UIAlertView的使用技巧

在使用uialertview的時候,初始化時使用方法 instancetype title message delegate id delegate cancelbuttontitle otherbuttontitles,會給乙個title和message。有時大家不需要title就會直接給mess...

iOS經典講解之清除快取檔案

清除快取之前先要計算檔案的大小 計算目錄大小 cgfloat foldersizeatpath nsstring path 將大小轉化為m return size 1024.0 1024.0 return 0 根據路徑刪除檔案 void cleancaches nsstring path 計算沙盒中...

iOS開發之常用的正規表示式

在 ios 中,我們使用 nspredicate 的字串比較功能來進行正規表示式處理,其比較關鍵字為 matches 下面,列舉乙個匹配6 15個由字母 數字組成的字串的正規表示式,來看看 nspredicate 的具體使用 nsstring regex a za z0 9 nspredicate ...