[ios]iOS 中正则表达式的使用

iOS 中正则表达式的使用

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

 

在 iOS 中,我们使用 NSPredicate 的字符串比较功能来进行正则表达式处理,其比较关键字为:MATCHES

 

下面,列举一个匹配6-15个由字母/数字组成的字符串的正则表达式,来看看 NSPredicate 的具体使用:

 

  1. NSString * regex        = @"(^[A-Za-z0-9]{6,15}$)";  
  2. NSPredicate * pred      = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
  3. BOOL isMatch            = [pred evaluateWithObject:@"123456ABCde"];  

猜你喜欢

转载自poolo.iteye.com/blog/1821036
今日推荐