iOS 正規表示式

2021-07-03 03:55:57 字數 4526 閱讀 8584

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

系統自帶的, 如: nspredicate, rangeofstring:option, nsregularexpression

regexkitlite regexkitlite 是乙個輕量級的 objective-c 的正規表示式庫,支援 mac os x 和 ios,使用 icu 庫開發。

至於regexkitlite, 這裡不做介紹。著重介紹系統自帶的那幾個辦法。

55分鐘學會正規表示式

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

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

簡述:cocoa框架中的nspredicate用於查詢,原理和用法都類似於sql中的where,作用相當於資料庫的過濾取。

nspredicate *predicate = [nspredicate predicatewithformat:(nsstring *), ...];
其中, 常見的format有:

(1) 比較運算子: >, <, ==, >=, <=, !=

例:@"number > 100"
(2) 範圍運算子: in, between

例:@"number between "

@"address in "

(3) 字串本身: self

(4) 字串相關: beginswith, endswith, contains

例:@"name contains[cd] 'ang'"  //包含某個字串

@"name beginswith[c] 'sh'" //以某個字串開頭

@"name endswith[d] 'ang'" //以某個字串結束

注:[c]不區分大小寫

[d]不區分發音符號即沒有重音符號

[cd]既不區分大小寫,也不區分發音符號。

(5) 萬用字元: like

例:@"name like[cd] '*er*'"    //*代表萬用字元,like也接受[cd].

@"name like[cd] '???er*'"

(6) 正規表示式: matches

例:nsstring *regex = @"^a.+e$";   //以a開頭,e結尾

@"name matches %@",regex

至於如何使用呢? 下面舉幾個例子:

(a) 對nsarray進行過濾, 帥選出包含」ang」的項

nsarray *array = [[nsarray alloc]initwithobjects:@"beijing", @"shanghai", @"guangzou", @"wuhan", nil];

nsstring *string = @"ang";

nspredicate *pred = [nspredicate predicatewithformat:@"self contains %@", string];

nslog(@"%@", [array filteredarrayusingpredicate:pred]);

// 列印結果:

// (

// shanghai,

// guangzou

// )

(b) 對nsdate進行篩選

//日期在十天之內:

nsdate *enddate = [nsdate date];

nstimeinterval timeinterval= [enddate timeintervalsincereferencedate];

timeinterval -=3600*24*10;

nsdate *begindate = [nsdate datewithtimeintervalsincereferencedate:timeinterval];

//對coredata進行篩選(假設有fetchrequest)

nspredicate *predicate_date = [nspredicate predicatewithformat:@"date >= %@ and date <= %@", begindate,enddate];

[fetchrequest setpredicate:predicate_date];

ok,nspredicate的功能很多, 也很強大。這裡暫時就點到此, 感興趣的可以自己一一試驗。 下面舉兩個例子說明一下如何使用正則。

// 判斷是否是有效郵箱

- (bool)isvalidateemail:(nsstring *)email";

nspredicate *predicate = [nspredicate predicatewithformat:@"self matches %@", regex];

return [predicate evaluatewithobject:email];

}// 判斷字串首字母是否為字母

- (bool)isstartedwithword:(nsstring *)astring

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

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

if (range.location != nsnotfound)

// 列印結果:

// typically from a nib.

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

詳細了解: ios 正規表示式 nsregularexpression

上面那篇文章總結的很不錯. 這裡簡單再舉個例子:

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)

// 列印結果:

// typically from a nib.

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

針對以上3種方式, 做乙個小小總結

第一種匹配需要學習nspredicate的寫法,需要查閱蘋果相關技術文件;

如果只關心第乙個匹配的結果,第二種匹配較為簡潔;

如果需要匹配多個結果,同時匹配多次,第三種方式效率會更高。

參考: ios常用正規表示式

表示式作用[\u4e00-\u9fa5]

匹配中文字元

[^\x00-\xff]

匹配雙位元組字元(包括漢字在內)

\n\s*\r

匹配空白行

<(\s*?)[^>]*>.*?|<.*? />

匹配html標記

^\s*|\s*$

匹配首尾空白字元

\w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*

匹配email地

[a-za-z]+://[^\s]*

匹配**url

\d-\d|\d-\d

匹配國內**號碼,匹配形式如 0511-4405222 或 021-87888822

[1-9]\d(?!\d)

\d+.\d+.\d+.\d+

匹配ip位址

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 開始...

iOS 正規表示式

1.前言 正規表示式是對字串操作的一種邏輯公式,就是用事先定義好的一些特定字元以及這些特定字元的組合,組成乙個規則字串,這個規則字串用來表達對字串的一種過濾邏輯。常見的用處就是匹配字串的合法性,擷取特定的字串等等。2.常見語法 語法說明 表示式例項 完整匹配的字串 一般字元 匹配自身 kity ki...

iOS正規表示式

包含數字和字母的密碼長度6 16位 bool validatepassword nsstring password 密碼正規表示式 nsstring passwordregex a za z 0 9 a za z0 9 nspredicate passwordtest nspredicate pre...