iPhone開發中用第三方工具RegexKitLite實現正則表達式
iPhone開發中用第三方工具RegexKitLite實現正則表達式是本文要介紹的內容,關于正則表達式,前面也接觸到了幾種,具體內容實現先來看本文詳解。
1、去RegexKitLite下 載類庫,解壓出來會有一個例子包及2個文件,其實用到的就這2個文件,添加到工程中。
2、工程中添加libicucore.dylib frameworks。
3、現在所有的nsstring對象就可以調用RegexKitLite中的方法了。
- NSString *email = @”kkk@aaa.com”;
- [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);
- // 2008-10-15 08:52:52.500 host_port[8021:807] portInteger: ‘8080′
取 string中http的例子。
小結:iPhone開發中用第三方工具RegexKitLite實現正則表達式的內容介紹完了,希望通過本文的學習能對你有所幫助!