緣起:
想獲取字符串中指定的字符,考慮用正則表達式,遂寫了如下的代碼:
- NSString *htmlStr = @"oauth_token=1a1de4ed4fca40599c5e5cfe0f4fba97&oauth_token_secret=3118a84ad910967990ba50f5649632fa&name=foolshit";
- NSString *regexString = @"oauth_token=(\\w+)&oauth_token_secret=(\\w+)&name=(\\w+)";
- NSString *matchedString1 = [htmlStr stringByMatching:regexString capture:1L];
- NSString *matchedString2 = [htmlStr stringByMatching:regexString capture:2L];
- NSString *matchedString3 = [htmlStr stringByMatching:regexString capture:3L];
獲取的結果如下:
1a1de4ed4fca40599c5e5cfe0f4fba97
3118a84ad910967990ba50f5649632fa
foolshit
思考:
雖然完成了任務,但是這么寫顯然是低效的,因為每次獲取都需要啟用正則表達式。所以改進如下:
- NSArray *matchArray = NULL;
- matchArray = [htmlStr componentsMatchedByRegex:regexString];
- NSLog(@"matchedString0 is %@", [matchArray objectAtIndex:0]);
- NSLog(@"matchedString1 is %@", [matchArray objectAtIndex:1]);
- NSLog(@"matchedString2 is %@", [matchArray objectAtIndex:2]);
- NSLog(@"matchedString3 is %@", [matchArray objectAtIndex:3]);
獲取的結果如下:
oauth_token=1a1de4ed4fca40599c5e5cfe0f4fba97&oauth_token_secret=3118a84ad910967990ba50f5649632fa&name=foolshit
1a1de4ed4fca40599c5e5cfe0f4fba97
3118a84ad910967990ba50f5649632fa
foolshit
注:
我想通過 $1,$2⋯⋯,這種形式獲取,但是沒有成功。不知道哪位高手能實現。
附錄:忘記寫需要加入第三方類庫了,⊙﹏⊙b汗,多虧有人留言提醒。
1.下載RegexKitLite類庫,解壓出來會有一個例子包及2個文件(RegexKitLite文件夾下的兩個文件),其實用到的就這2個文件,添加到工程中。
2.工程中添加libicucore.dylib frameworks。
轉:http://blog.csdn.net/zcl369369/article/details/7181807
下面結論親測准確,可直接復用
/*** htmlStr 待解析字符串;regex 正則表達式;result 正則匹配結果 ***/ 一、只匹配成功一個的情形 1、結論:stringByMatching 方法返回的結果包括匹配條件T和F。而且即使待解析的字符串中有多處匹配,但也只返回第一個匹配成功的。 NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSString *result = [htmlStr stringByMatching:regex]; NSLog(@"匹配結果:%@",result); 匹配結果:T1F 2、結論 [htmlStr stringByMatching:regex capture:0L] 與 [htmlStr stringByMatching:regex] 效果一樣。 [htmlStr stringByMatching:regex capture:1L] 返回第一個匹配的結果,不帶匹配的兩端T和F。 [htmlStr stringByMatching:regex capture:2L] 提示capture 越界。 ---- NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSString *result = [htmlStr stringByMatching:regex capture:0L]; NSLog(@"匹配結果:%@",result); 匹配結果:T1F ---- NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSString *result = [htmlStr stringByMatching:regex capture:1L]; NSLog(@"匹配結果:%@",result); 匹配結果:1 二、成功匹配多個結果的情形 1、結論:componentsMatchedByRegex 匹配返回的字符串帶着兩端的T和F NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSArray *result = [htmlStr componentsMatchedByRegex:regex]; NSLog(@"匹配結果:%@",result); 匹配結果: ( T1F, T2F, T3F ) 2、結論:componentsMatchedByRegex: capture: 方法,當capture值為0L時,返回結果與componentsMatchedByRegex相同。當capture值為1L時,返回T和F中間的內容,不帶T和F。當capture值為2L時,系統提示越界了。 NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSArray *result = [htmlStr componentsMatchedByRegex:regex capture:0L]; NSLog(@“匹配結果:%@“,result); 匹配結果: ( T1F, T2F, T3F ) --- NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSArray *result = [htmlStr componentsMatchedByRegex:regex capture:1L]; NSLog(@"匹配結果:%@",result); 匹配結果: ( 1, 2, 3 ) --- NSString *htmlStr = @"0_T1F_0_T2F_0_T3F_0"; NSString *regex = @"T([\\s\\S]*?)F"; NSArray *result = [htmlStr componentsMatchedByRegex:regex capture:2L]; NSLog(@"匹配結果:%@",result); 錯誤提示:*** -[__NSCFConstantString componentsMatchedByRegex:capture:]: The capture argument is not valid.'