RegexKitLite学习

http://blog.sina.com.cn/s/blog_71715bf8010165t0.html

准备工作,下载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: 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: 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.    
  13. NSLog(@"capturesArray: %@" capturesArray);  
  14. 输出结果:  
  15. shell% ./capturesArray↵  
  16. 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray:  
  17.         
  18.        "$10.23"  
  19.        "10.23"  
  20.        10,  
  21.        23  
  22.    ),  
  23.         
  24.        "$1024.42"  
  25.        "1024.42"  
  26.        1024,  
  27.        42  
  28.    ),  
  29.         
  30.        "$3099"  
  31.        3099,  
  32.        3099,  
  33.        ""   
  34.     
  35.  
  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.   
  13. NSLog(@"capturesArray: %@"capturesArray);  
  14. 输出结果:  
  15. shell% ./capturesArray↵  
  16. 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray:  
  17.         
  18.        "$10.23" 
  19.        "10.23" 
  20.        10,  
  21.        23  
  22.    ),  
  23.         
  24.        "$1024.42" 
  25.        "1024.42" 
  26.        1024,  
  27.        42  
  28.    ),  
  29.         
  30.        "$3099" 
  31.        3099,  
  32.        3099,  
  33.        ""  
  34.     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值