ios下最简单的正则,RegexKitLite

引用
1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中。
备用地址:
http://www.cocoachina.com/bbs/job.php?action-download-pid-135286-tid-18111-aid-11143.html

2.工程中添加libicucore.dylib frameworks。

3.现在所有的nsstring对象就可以调用RegexKitLite中的方法了。

NSString *email = @”[email protected]”;

[email isMatchedByRegex:@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b”];

返回YES,证明是email格式,需要注意的是RegexKitLite用到的正则表达式和wiki上的略有区别。

searchString = @”http://www.example.com:8080/index.html”;

regexString  = @”\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?”;

NSInteger portInteger = [[searchString stringByMatching:regexString capture:1L] integerValue];

NSLog(@”portInteger: ‘%ld’”, (long)portInteger);


猜你喜欢

转载自iaiai.iteye.com/blog/2178842