现在的位置: 首页 > 综合 > 正文

iOS中使用RegexKitLite来试用正则表达式

2013年08月02日 ⁄ 综合 ⁄ 共 6554字 ⁄ 字号 评论关闭

iOS中使用RegexKitLite来试用正则表达式
收藏

准备工作,下载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' //生成子字符串
      

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'
      

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}.'
      

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);  

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.    )  

抱歉!评论已关闭.