iOS中使用RegexKitLite来试用正则表达式
转:http://blog.csdn.net/nullcn/article/details/6338592
准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中。
然后还要加载framework libicucore.dylib ,因为RegexKitLite是调用这个里面的API,苹果规定过不能使用私有的api和没有发布的api。实际上RegexKitLite对NSString做了扩展,目前只支持NSString,对我来说也够了...
基本使用的例子(更多信息参看 官方文档)
1.
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
- NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
- NSError *error = NULL;
- matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
- NSLog(@"matchedRange: %@" , NSStringFromRange(matchedRange));
- // 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
- NSString *matchedString = [searchString substringWithRange:matchedRange];
- NSLog(@"matchedString: '%@'" , matchedString);
- // 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
- NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
- NSError *error = NULL;
- matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
- NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));
- // 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
- NSString *matchedString = [searchString substringWithRange:matchedRange];
- NSLog(@"matchedString: '%@'", matchedString);
- // 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
2.找到第一个匹配并返回一个NSString
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
- NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
- NSLog(@"matchedString: '%@'" , matchedString);
- // 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
- NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
- NSLog(@"matchedString: '%@'", matchedString);
- // 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
3.查找和替换,加括号和概念和Python中的一样,$1指代第一个括号中的内容
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"//b(//w+)//b" ;
- NSString *replaceWithString = @"{$1}" ;
- NSString *replacedString = NULL;
- replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
- //NSMutableString可以直接替换,并返回替换的次数
- NSLog(@"replaced string: '%@'" , replacedString);
- // 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
- NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat." ];
- NSString *regexString = @"//b(//w+)//b" ;
- NSString *replaceWithString = @"{$1}" ;
- NSUInteger replacedCount = 0UL;
- replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
- NSLog(@"count: %lu string: '%@'" , (u_long)replacedCount, mutableString);
- // 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"//b(//w+)//b";
- NSString *replaceWithString = @"{$1}";
- NSString *replacedString = NULL;
- replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
- //NSMutableString可以直接替换,并返回替换的次数
- NSLog(@"replaced string: '%@'", replacedString);
- // 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
- NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat."];
- NSString *regexString = @"//b(//w+)//b";
- NSString *replaceWithString = @"{$1}";
- NSUInteger replacedCount = 0UL;
- replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
- NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);
- // 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
4.用于拆分,返回一个拆分后的字符串数组
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"//s+" ;
- NSArray *splitArray = NULL;
- splitArray = [searchString componentsSeparatedByRegex:regexString];
- // splitArray == { @"This", @"is", @"neat." }
- NSLog(@"splitArray: %@" , splitArray);
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"//s+";
- NSArray *splitArray = NULL;
- splitArray = [searchString componentsSeparatedByRegex:regexString];
- // splitArray == { @"This", @"is", @"neat." }
- NSLog(@"splitArray: %@", splitArray);
5.返回所有匹配的字符串数组,这个例子中虽然有多个括号,但是 componentsMatchedByRegex不管
- NSString *searchString = @ "$10.23, $1024.42, $3099" ;
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
- NSArray *matchArray = NULL;
- matchArray = [searchString componentsMatchedByRegex:regexString];
- // matchArray == { @"$10.23", @"$1024.42", @"$3099" };
- NSLog(@"matchArray: %@" , matchArray);
- 6.返回所有匹配的字符串数组处理所有的括号
- NSString *searchString = @"$10.23, $1024.42, $3099" ;
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
- NSArray *capturesArray = NULL;
- capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
- /* capturesArray ==
- [NSArray arrayWithObjects:
- [NSArray arrayWithObjects: @"$10.23", @"10.23", @"10", @"23", NULL],
- [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],
- [NSArray arrayWithObjects: @"$3099", @"3099", @"3099", @"", NULL],
- NULL];
- */
- NSLog(@"capturesArray: %@" , capturesArray);
- 输出结果:
- shell% ./capturesArray↵
- 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
- (
- "$10.23" ,
- "10.23" ,
- 10,
- 23
- ),
- (
- "$1024.42" ,
- "1024.42" ,
- 1024,
- 42
- ),
- (
- "$3099" ,
- 3099,
- 3099,
- ""
- )
- )
- NSString *searchString = @"$10.23, $1024.42, $3099";
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
- NSArray *matchArray = NULL;
- matchArray = [searchString componentsMatchedByRegex:regexString];
- // matchArray == { @"$10.23", @"$1024.42", @"$3099" };
- NSLog(@"matchArray: %@", matchArray);
- 6.返回所有匹配的字符串数组处理所有的括号
- NSString *searchString = @"$10.23, $1024.42, $3099";
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
- NSArray *capturesArray = NULL;
- capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
- /* capturesArray ==
- [NSArray arrayWithObjects:
- [NSArray arrayWithObjects: @"$10.23", @"10.23", @"10", @"23", NULL],
- [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],
- [NSArray arrayWithObjects: @"$3099", @"3099", @"3099", @"", NULL],
- NULL];
- */
- NSLog(@"capturesArray: %@", capturesArray);
- 输出结果:
- shell% ./capturesArray↵
- 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
- (
- "$10.23",
- "10.23",
- 10,
- 23
- ),
- (
- "$1024.42",
- "1024.42",
- 1024,
- 42
- ),
- (
- "$3099",
- 3099,
- 3099,
- ""
- )
- )
iOS中使用RegexKitLite来试用正则表达式的更多相关文章
- iOS中使用RegexKitLite来试用正则表达式 使用ARC 20个错误解决办法
You can also disable the ARC for the RegexKitLite only by adding a flag: select the project -> YO ...
- 正则表达式在iOS中的运用
1.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- iOS中3种正则表达式的使用与比较
正则表达式在用户注册和登录中应用很广,通过正则表达式可以判断用户输入的数据正确与否. 在iOS4.0以前开发者一般是通过谓词(NSPredicate)和加入正则表达式的第三方库(如:RegexKitL ...
- iOS中运用正则表达式
iOS中运用正则表达式来匹配短信验证码,电话号码,邮箱等是比较常见的. 在iOS中运用正则表达式主要有三种方式: -:通过谓词下面是实例代码: - (BOOL)regularExpresionWith ...
- iOS中使用正则
一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- iOS 中的 NSTimer
iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...
- iOS 中的Push Notifications简单实现(APNS)
Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...
- iOS中引用计数内存管理机制分析
在 iOS 中引用计数是内存的管理方式,虽然在 iOS5 版本中,已经支持了自动引用计数管理模式,但理解它的运行方式有助于我们了解程序的运行原理,有助于 debug 程序. 操作系统的内存管理分成堆和 ...
- 关于ios中的文本操作-简介
来源:About Text Handling in iOS 官方文档 iOS平台为我们提供了许多在app中展示文本和让用户编辑文本的方式.同时,它也允许你在app视图中展示格式化的文本和网页内容.你可 ...
随机推荐
- oracle赋某表truncate权限
一.ins用户下创建存储过程 二.mobapp用户下赋权:执行存储过程的权限 三.odso_insert用户下执行存储过程,即可truncate表 查看odso_insert用户拥有的权限 一.ins ...
- xunsearch 迅搜初探
2014年1月2日 19:34:12 [root@localhost bin]# ./php /usr/local/lamp/xunsearch/sdk/php/util/Quest.php demo ...
- google地图的url参数
Google Maps Intents for Android The Google Maps app for Android exposes several intents that you can ...
- Long-Polling, Websockets, SSE(Server-Sent Event) 之间有什么区别?
链接:http://www.mamicode.com/info-detail-1327667.html https://www.jianshu.com/p/d3f66b1eb748?from=time ...
- 局域网搭建https局域网
局域网搭建https局域网 1.使用tomcat作为服务器搭建局域网访问https 需要使用java jdk\bin下的keytool.exe来创建证书 使用命令:keytool -genkenpai ...
- 【转】MySQL安全配置介绍
一.前言 很多文章中会说,数据库的权限按最小权限为原则,这句话本身没有错,但是却是一句空话.因为最小权限,这个东西太抽象,很多时候你并弄不清楚具体他需要哪些权限. 现在很多mysql用着root账户在 ...
- Git(三)Git的远程仓库
一. 添加远程库 现在我们已经在本地创建了一个Git仓库,又想让其他人来协作开发,此时就可以把本地仓库同步到远程仓库,同时还增加了本地仓库的一个备份.常用的远程仓库就是github:https://g ...
- 自然语言处理---用隐马尔科夫模型(HMM)实现词性标注---1998年1月份人民日报语料---learn---test---evaluation---Demo---java实现
先放上一张Demo的测试图 测试的句子及每个分词的词性标注为: 目前/t 这/rzv 条/q 高速公路/n 之间/f 的/ude1 路段/n 已/d 紧急/a 封闭/v ./w 需要基础知识 HM ...
- Spring Security + JWT实现前后端分离权限认证
现在国内前后端很多公司都在使用前后端分离的开发方式,虽然也有很多人并不赞同前后端分离,比如以下这篇博客就很有意思: https://www.aliyun.com/jiaocheng/650661.ht ...
- 大纲2.3 Internet
Internet:域名系统基础知识和配置,上网查询访问的方法,常用电子邮件的种类和收发电子邮件的方法,网络信息搜索,网络信息下载.上传的基本方法,网络信息共享方法. DNS域名系统 域名 不区分大小写 ...