iOS RegexKitLite 提取匹配的內容

 

   
   

使用RegexKitLite正則表達式須要如下工做:
1.RegexKitLite官方網址(內含使用教程):http://regexkit.sourceforge.net/RegexKitLite
2.下載地址:http://downloads.sourceforge.net/regexkit/RegexKitLite-4.0.tar.bz2
3.將RegexKitLite.hRegexKitLite.m引入到工程中。
4.引入"libicucore.dylib"框架
5.須要在處理的類中導入"RegexKitLite.h"

言歸正傳,用正則表達式提取匹配的內容(此例子中講述匹配字符串中的手機號碼):
NSString *searchString = @"15200000000,1234,12012345678,13400000000"; // 要提取的字符串
NSString *regexString 正則表達式

= @"\\b(13|15|18)([0-9]{9})\\b";  // 手機號碼的正則表達式(因爲15和18號段的不太熟悉,索性就乾脆都算上了,你懂的。)
NSArray  *matchArray  = [searchString componentsMatchedByRegex:regexString];  // 匹配結果以數組形式返回 NSLog(@"%@",matchArray); // 進行輸出 NSLog(@"%@",matchArray); 輸出結果以下: 2011-08-23 14:39:23.356 ZQRegex[6726:207] (     15200000000,     13400000000 )
相關文章
相關標籤/搜索