iOS中使用RegexKitLite來試用Regex

來源:互聯網
上載者:User

標籤:

轉: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‘ //產生子字串   
[cpp] view plaincopy 
  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‘   
[cpp] view plaincopy 
  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}.‘   
[cpp] view plaincopy 
  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);  
[cpp] view plaincopy 
  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. )  
[cpp] view plaincopy 
  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來試用Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.