转:http://blog.csdn.net/nullcn/article/details/6338592

准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中。

然后还要加载framework libicucore.dylib ,因为RegexKitLite是调用这个里面的API,苹果规定过不能使用私有的api和没有发布的api。实际上RegexKitLite对NSString做了扩展,目前只支持NSString,对我来说也够了...

基本使用的例子(更多信息参看 官方文档) 
1.

  1. NSString *searchString = @ "This is neat." ;
  2. NSString *regexString  = @"(//w+)//s+(//w+)//s+(//w+)" ;
  3. NSRange   matchedRange = NSMakeRange(NSNotFound, 0UL);
  4. NSError  *error        = NULL;
  5. matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
  6. NSLog(@"matchedRange: %@" , NSStringFromRange(matchedRange));
  7. // 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
  8. NSString *matchedString = [searchString substringWithRange:matchedRange];
  9. NSLog(@"matchedString: '%@'" , matchedString);
  10. // 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
  1. NSString *searchString = @"This is neat.";
  2. NSString *regexString  = @"(//w+)//s+(//w+)//s+(//w+)";
  3. NSRange   matchedRange = NSMakeRange(NSNotFound, 0UL);
  4. NSError  *error        = NULL;
  5. matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
  6. NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));
  7. // 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
  8. NSString *matchedString = [searchString substringWithRange:matchedRange];
  9. NSLog(@"matchedString: '%@'", matchedString);
  10. // 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串

2.找到第一个匹配并返回一个NSString

  1. NSString *searchString  = @ "This is neat." ;
  2. NSString *regexString   = @"(//w+)//s+(//w+)//s+(//w+)" ;
  3. NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
  4. NSLog(@"matchedString: '%@'" , matchedString);
  5. // 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
  1. NSString *searchString  = @"This is neat.";
  2. NSString *regexString   = @"(//w+)//s+(//w+)//s+(//w+)";
  3. NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
  4. NSLog(@"matchedString: '%@'", matchedString);
  5. // 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'

3.查找和替换,加括号和概念和Python中的一样,$1指代第一个括号中的内容

  1. NSString *searchString      = @ "This is neat." ;
  2. NSString *regexString       = @"//b(//w+)//b" ;
  3. NSString *replaceWithString = @"{$1}" ;
  4. NSString *replacedString    = NULL;
  5. replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
  6. //NSMutableString可以直接替换,并返回替换的次数
  7. NSLog(@"replaced string: '%@'" , replacedString);
  8. // 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
  9. NSMutableString *mutableString     = [NSMutableString stringWithString:@"This is neat." ];
  10. NSString        *regexString       = @"//b(//w+)//b" ;
  11. NSString        *replaceWithString = @"{$1}" ;
  12. NSUInteger       replacedCount     = 0UL;
  13. replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
  14. NSLog(@"count: %lu string: '%@'" , (u_long)replacedCount, mutableString);
  15. // 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
  1. NSString *searchString      = @"This is neat.";
  2. NSString *regexString       = @"//b(//w+)//b";
  3. NSString *replaceWithString = @"{$1}";
  4. NSString *replacedString    = NULL;
  5. replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
  6. //NSMutableString可以直接替换,并返回替换的次数
  7. NSLog(@"replaced string: '%@'", replacedString);
  8. // 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
  9. NSMutableString *mutableString     = [NSMutableString stringWithString:@"This is neat."];
  10. NSString        *regexString       = @"//b(//w+)//b";
  11. NSString        *replaceWithString = @"{$1}";
  12. NSUInteger       replacedCount     = 0UL;
  13. replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
  14. NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);
  15. // 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'

4.用于拆分,返回一个拆分后的字符串数组

  1. NSString *searchString = @ "This is neat." ;
  2. NSString *regexString  = @"//s+" ;
  3. NSArray  *splitArray   = NULL;
  4. splitArray = [searchString componentsSeparatedByRegex:regexString];
  5. // splitArray == { @"This", @"is", @"neat." }
  6. NSLog(@"splitArray: %@" , splitArray);
  1. NSString *searchString = @"This is neat.";
  2. NSString *regexString  = @"//s+";
  3. NSArray  *splitArray   = NULL;
  4. splitArray = [searchString componentsSeparatedByRegex:regexString];
  5. // splitArray == { @"This", @"is", @"neat." }
  6. NSLog(@"splitArray: %@", splitArray);

5.返回所有匹配的字符串数组,这个例子中虽然有多个括号,但是 componentsMatchedByRegex不管

  1. NSString *searchString = @ "$10.23, $1024.42, $3099" ;
  2. NSString *regexString  = @"//$((//d+)(?://.(//d+)|//.?))" ;
  3. NSArray  *matchArray   = NULL;
  4. matchArray = [searchString componentsMatchedByRegex:regexString];
  5. // matchArray == { @"$10.23", @"$1024.42", @"$3099" };
  6. NSLog(@"matchArray: %@" , matchArray);
  7. 6.返回所有匹配的字符串数组处理所有的括号
  8. NSString *searchString  = @"$10.23, $1024.42, $3099" ;
  9. NSString *regexString   = @"//$((//d+)(?://.(//d+)|//.?))" ;
  10. NSArray  *capturesArray = NULL;
  11. capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
  12. /* capturesArray ==
  13. [NSArray arrayWithObjects:
  14. [NSArray arrayWithObjects:  @"$10.23",   @"10.23",   @"10", @"23", NULL],
  15. [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],
  16. [NSArray arrayWithObjects:   @"$3099",    @"3099", @"3099",   @"", NULL],
  17. NULL];
  18. */
  19. NSLog(@"capturesArray: %@" , capturesArray);
  20. 输出结果:
  21. shell% ./capturesArray↵
  22. 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
  23. (
  24. "$10.23" ,
  25. "10.23" ,
  26. 10,
  27. 23
  28. ),
  29. (
  30. "$1024.42" ,
  31. "1024.42" ,
  32. 1024,
  33. 42
  34. ),
  35. (
  36. "$3099" ,
  37. 3099,
  38. 3099,
  39. ""
  40. )
  41. )
  1. NSString *searchString = @"$10.23, $1024.42, $3099";
  2. NSString *regexString  = @"//$((//d+)(?://.(//d+)|//.?))";
  3. NSArray  *matchArray   = NULL;
  4. matchArray = [searchString componentsMatchedByRegex:regexString];
  5. // matchArray == { @"$10.23", @"$1024.42", @"$3099" };
  6. NSLog(@"matchArray: %@", matchArray);
  7. 6.返回所有匹配的字符串数组处理所有的括号
  8. NSString *searchString  = @"$10.23, $1024.42, $3099";
  9. NSString *regexString   = @"//$((//d+)(?://.(//d+)|//.?))";
  10. NSArray  *capturesArray = NULL;
  11. capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
  12. /* capturesArray ==
  13. [NSArray arrayWithObjects:
  14. [NSArray arrayWithObjects:  @"$10.23",   @"10.23",   @"10", @"23", NULL],
  15. [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],
  16. [NSArray arrayWithObjects:   @"$3099",    @"3099", @"3099",   @"", NULL],
  17. NULL];
  18. */
  19. NSLog(@"capturesArray: %@", capturesArray);
  20. 输出结果:
  21. shell% ./capturesArray↵
  22. 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
  23. (
  24. "$10.23",
  25. "10.23",
  26. 10,
  27. 23
  28. ),
  29. (
  30. "$1024.42",
  31. "1024.42",
  32. 1024,
  33. 42
  34. ),
  35. (
  36. "$3099",
  37. 3099,
  38. 3099,
  39. ""
  40. )
  41. )

iOS中使用RegexKitLite来试用正则表达式的更多相关文章

  1. iOS中使用RegexKitLite来试用正则表达式 使用ARC 20个错误解决办法

    You can also disable the ARC for the RegexKitLite only by adding a flag: select the project -> YO ...

  2. 正则表达式在iOS中的运用

    1.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  3. iOS中3种正则表达式的使用与比较

    正则表达式在用户注册和登录中应用很广,通过正则表达式可以判断用户输入的数据正确与否. 在iOS4.0以前开发者一般是通过谓词(NSPredicate)和加入正则表达式的第三方库(如:RegexKitL ...

  4. iOS中运用正则表达式

    iOS中运用正则表达式来匹配短信验证码,电话号码,邮箱等是比较常见的. 在iOS中运用正则表达式主要有三种方式: -:通过谓词下面是实例代码: - (BOOL)regularExpresionWith ...

  5. iOS中使用正则

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  6. iOS 中的 NSTimer

    iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...

  7. iOS 中的Push Notifications简单实现(APNS)

    Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...

  8. iOS中引用计数内存管理机制分析

    在 iOS 中引用计数是内存的管理方式,虽然在 iOS5 版本中,已经支持了自动引用计数管理模式,但理解它的运行方式有助于我们了解程序的运行原理,有助于 debug 程序. 操作系统的内存管理分成堆和 ...

  9. 关于ios中的文本操作-简介

    来源:About Text Handling in iOS 官方文档 iOS平台为我们提供了许多在app中展示文本和让用户编辑文本的方式.同时,它也允许你在app视图中展示格式化的文本和网页内容.你可 ...

随机推荐

  1. NET调用Com组件事例

    http://blog.csdn.net/shizhiyingnj/article/details/1507948 在程序设计中,往往通过键盘的某个按键来完成相关操作! 下面就来说明如何实现: 1.引 ...

  2. Linix下修改mysql服务器编码

    1. 找到mysql的配置文件,拷贝到etc目录下,第一步很重要 把/usr/share/doc/mysql-server-5.1.52/my-large.cnf 复制到 /etc/my.cnf 即用 ...

  3. 下载vc++运行库

    之前下载vc++运行库都是百度,从中关村.当下等软件网站下载,但是最近这些网站涉及到安全问题,所以从官网下载比较合适 微软官网-中文 在搜索中 搜索vc++2010/2015等,搜索结果中找到xxxx ...

  4. 【PAT】1019 数字黑洞 (20)(20 分)

    1019 数字黑洞 (20)(20 分) 给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做, ...

  5. 019.Zabbix的Trigger及相关函数

    一 告警简介 告警指将达到某一个阀值事件的消息发送给用户,让用户在事件发生的时候即可知道监控项处于不正常状态,从而采取相应的措施.在Zabbix中,高进是由一系列的流程组成,首先是触发器达到阀值,接下 ...

  6. 记一次重装系统(.net开发环境重装问题汇总)

    起因: 有一天,我突然感觉到电脑的运行明显变慢,慢的可怕,启动任务资源管理器一看,不看不知道,一看吓一跳,CPU使用率,物理内存皆100%,当时的第一印象,是电脑中病毒了吧!!,进入进程一看,有几个名 ...

  7. odoo视图继承

    odoo视图继承 简介继承 在odoo里可以通过继承的方式来改写已经存在的view 对比view的定义视图定义中写法多了inherit_id inherit_id指向要改写的view <fiel ...

  8. “百度杯”CTF比赛 九月场 YeserCMS

    打开题目 进入后是一个cms,但肯定的是这个cms不叫yesercms 于是我们开始随便翻翻,寻找信息,后台我也看了除了一个登陆界面,就没有其他的提示信息. 最后在文档下载的评论栏里发现,这个cms的 ...

  9. EditText 数字范围 检查string 是不是数字

    public static boolean isNumeric00(String str){ try{ Integer.parseInt(str); return true; }catch(Numbe ...

  10. 1257: [CQOI2007]余数之和

    题目链接 bzoj1257: [CQOI2007]余数之和 题解 数论分块,乘等差数列求和 代码 #include<bits/stdc++.h> using namespace std; ...