NSPredicate匹配中文正

在 iOS 中,我們使用 NSPredicate 的字符串比較功能來進行正則表達式處理,其比較關鍵字爲:MATCHES

下面,列舉一個匹配6-15個由字母/數字組成的字符串的正則表達式,來看看 NSPredicate 的具體使用:

    

NSString * regex        = @"(^[A-Za-z0-9]{6,15}$)";

NSPredicate * pred      = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

BOOL isMatch            = [pred evaluateWithObject:@"123456ABCde"];


iOS 中可以通過NSPredicate 來處理正則表達式。相關資料如下:

NSPredicate 蘋果官方文檔:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/predicates.html

Predicate format strings:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html

ICU 正則表達式規則:
http://www.icu-project.org/userguide/regexp.html

發佈了20 篇原創文章 · 獲贊 5 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章