`
119568242
  • 浏览: 420181 次
  • 性别: Icon_minigender_1
  • 来自: 深圳/湛江
社区版块
存档分类
最新评论

[ios]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"];  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics