IOS正则表达式

来源:互联网 发布:淘宝hd历史版本 ios 编辑:程序博客网 时间:2024/05/22 00:52

在 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

0 0
原创粉丝点击