iOS中使用RegexKitLite來試用Regex
收藏
準備工作,下載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.";<br />NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";<br />NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);<br />NSError *error = NULL;<br />matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];<br />NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));<br />// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘<br />NSString *matchedString = [searchString substringWithRange:matchedRange];<br />NSLog(@"matchedString: '%@'", matchedString);<br />// 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.";<br />NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";<br />NSString *matchedString = [searchString stringByMatching:regexString capture:2L];<br />NSLog(@"matchedString: '%@'", matchedString);<br />// 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.";<br />NSString *regexString = @"//b(//w+)//b";<br />NSString *replaceWithString = @"{$1}";<br />NSString *replacedString = NULL;<br />replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];<br />//NSMutableString可以直接替換,並返回替換的次數<br />NSLog(@"replaced string: '%@'", replacedString);<br />// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'<br />NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat."];<br />NSString *regexString = @"//b(//w+)//b";<br />NSString *replaceWithString = @"{$1}";<br />NSUInteger replacedCount = 0UL;<br />replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];<br />NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);<br />// 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.";<br />NSString *regexString = @"//s+";<br />NSArray *splitArray = NULL;<br />splitArray = [searchString componentsSeparatedByRegex:regexString];<br />// splitArray == { @"This", @"is", @"neat." }<br />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";<br />NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";<br />NSArray *matchArray = NULL;<br />matchArray = [searchString componentsMatchedByRegex:regexString];<br />// matchArray == { @"$10.23", @"$1024.42", @"$3099" };<br />NSLog(@"matchArray: %@", matchArray);<br />6.返回所有匹配的字串數組處理所有的括弧<br />NSString *searchString = @"$10.23, $1024.42, $3099";<br />NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";<br />NSArray *capturesArray = NULL;<br />capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];<br />/* capturesArray ==<br />[NSArray arrayWithObjects:<br /> [NSArray arrayWithObjects: @"$10.23", @"10.23", @"10", @"23", NULL],<br /> [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],<br /> [NSArray arrayWithObjects: @"$3099", @"3099", @"3099", @"", NULL],<br /> NULL];<br />*/<br />NSLog(@"capturesArray: %@", capturesArray);<br />輸出結果:<br />shell% ./capturesArray↵<br />2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (<br /> (<br /> "$10.23",<br /> "10.23",<br /> 10,<br /> 23<br /> ),<br /> (<br /> "$1024.42",<br /> "1024.42",<br /> 1024,<br /> 42<br /> ),<br /> (<br /> "$3099",<br /> 3099,<br /> 3099,<br /> ""<br /> )<br />)