iPhone开发--正则表达式获取字符串中的内容
缘起:
想获取字符串中指定的字符,考虑用正则表达式,遂写了如下的代码:
- 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.'