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 正则匹配结果 ***/ 一、只匹配成功一个的情形
、结论: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 、结论
[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);
匹配结果: 二、成功匹配多个结果的情形 、结论: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
) 、结论: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);
匹配结果:
(
,
, ) --- 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.'
iPhone开发--正则表达式获取字符串中的内容的更多相关文章
- JS正则表达式获取字符串中特定字符
JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test 实 ...
- 正则表达式获取字符串中的img标签中的url链接
废话不多说直接看代码 JavaScript中的代码: var re = /src=\"([^\"]*?)\"/i; var arr = str.match(re); if ...
- oracle 使用正则表达式获取字符串中包含的数字
select REGEXP_REPLACE('字符串中包含的数字123,提取后就是123', '[^0-9]', '') from dual;
- JS 获取字符串中的url并返回其下标索引
//获取字符串中的url极其下标索引 function getHttpUrlArray(s) { var s1 = s.match(/http.*/); if(s1 == null) { return ...
- C# 获取字符串中的数字
/// <summary> /// 获取字符串中的数字(不包含小数点) /// </summary> /// <param name="str"> ...
- Java中用正则表达式截取字符串中
Java中用正则表达式截取字符串中第一个出现的英文左括号之前的字符串.比如:北京市(海淀区)(朝阳区)(西城区),截取结果为:北京市.正则表达式为() A ".*?(?=\\()" ...
- 获取字符串中img标签的url集合(转载)
/// <summary> /// 获取字符串中img的url集合 /// </summary> /// <param name="content"& ...
- 获取字符串中每个字符出现的次数(利用TreeMap)
案例:"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)分析1:定义一个字符串(可以改进为键盘录入)2:定义一个 ...
- C#,.net获取字符串中指定字符串的个数、所在位置与替换字符串
方法一: public static int indexOf (字符串/字符,int从第几位开始,int共查几位) string tests = "1absjjkcbfka2rsbcfak2 ...
随机推荐
- NEERC Southern Subregional 2011
NEERC Southern Subregional 2011 A - Bonnie and Clyde solution 双指针搞搞就好. 时间复杂度:\(O(n)\) B - Building F ...
- Python学习一|anaconda的安装问题以及Python语言的特点
安装时遇到的问题 安装anaconda3.0到D盘之后,配置好两个环境变量:D:\anaconda和D:\anaconda\Scripts.发现在命令行中执行python指令可以,但conda指令却是 ...
- idea 从数据库快速生成Spring Data JPA实体类
第一步,调出 Persistence 窗口. File—>Project Structure—>model—> + —>JPA 第二步:打开 Persistence窗口 配置 ...
- NTP服务放大攻击的解决办法
什么是NTP服务? 网络时间协议NTP(Network Time Protocol)是用于互联网中时间同步的标准互联网协议.NTP服务器通过NTP服务向网络上的计算机或其他设备提供标准的授时服务,以保 ...
- v-if案例解析(element-ui form-item 结合 v-if 动态生成rule规则\表单元素,表单无法验证问题剖析 )
fire 读在最前面: 1.此文章衔接Vue 虚拟Dom 及 部分生命周期初探,相关整体知识点请先阅读后再继续本文阅读 问:当v-if为true时,会重新渲染相关dom节点吗? <child v ...
- 安装Xampp-配置appche,mysql运行环境遇到的坑(转)
用php编写的web应用程序,需运行在php的web容器中,其中apache server是一个针对php web容器,它是apache下的开源项目.通常要运行一个web程序,我们还需要安装数据库软件 ...
- Ubuntu出现apt-get: Package has no installation candidate问题
今天在安装 vim 的时候出现了 Package 'vim' has no installation candidate的问题 解决方法如下:# apt-get update# apt-get upg ...
- jdk windows环境变量
(1)新建->变量名"JAVA_HOME",变量值"C:\Java\jdk1.8.0_05"(即JDK的安装路径) (2)编辑->变量名" ...
- NET 架构指南频道
NET 架构指南频道 微软在Visual Studio 2017 正式发布的时候也上线了一个参考应用https://github.com/dotnet/eShopOnContainers , 最近微软 ...
- vue-cli的工程模板与构建工具
vue-cli的工程模板与构建工具 https://www.cnblogs.com/yinn/p/9712480.html 脚手架vue-cli系列二:vue-cli的工程模板与构建工具 上篇文章我们 ...