在处理字符串的时候,常常会用到正则表达式,在iphone os上也不例外。使用 RegexKit Framework 就可以了。在这里下载RegexKitLite。
解压 RegexKitLite-4.0.tar.bz2 :
1 2 3 4 5 6 7 8 9 10 | RegexKitLite.h RegexKitLite.m RegexKitLite.html examples RKLMatchEnumerator.h RKLMatchEnumerator.m NSString-HexConversion.h NSString-HexConversion.m link_example.m main.m |
使用
这里,我们只需要 RegexKitLite.h 和 RegexKitLite.m 两个文件,将其加入到你的工程中。另外加入 -licucore 链接开关。
简单的例子如下:
1 2 3 4 5 6 7 | NSString *searchString = @"This is neat."; NSString *regexString = @"//b(//w+)//b"; NSString *replaceWithString = @"{$1}"; NSString *replacedString = NULL; replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString]; NSLog(@"replaced string: '%@'", replacedString); |
输出结果为:
1 | replaced string: '{This} {is} {neat}.' |
同时,也可以使用 Enumerator 来取得每个匹配的项。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #import <Foundation/NSAutoreleasePool.h> #import "RegexKitLite.h" #import "RKLMatchEnumerator.h" int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *searchString = @"one/ntwo/n/nfour/n"; NSEnumerator *matchEnumerator = NULL; NSString *regexString = @"(?m)^.*$"; NSLog(@"searchString: '%@'", searchString); NSLog(@"regexString : '%@'", regexString); matchEnumerator = [searchString matchEnumeratorWithRegex:regexString]; NSUInteger line = 0; NSString *matchedString = NULL; while((matchedString = [matchEnumerator nextObject]) != NULL) { NSLog(@"%d: %d '%@'", ++line, [matchedString length], matchedString); } [pool release]; return(0); } |
例子
解析HTML
下面用一个例子,来举例匹配HTML中字符串的方法。从img-tag中抽出alt属性的值。
1 2 | <img src="/img/icon_new_b.gif" alt="test1" width="13" height="13" /> <img src="/img/icon_news_b.gif" alt="test2" width="13" height="13" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | NSString *details = [item objectForKey:@"description"]; if ([details length] > 0) { NSString *searchString = [details stringByHalfwideningLatinCharacters]; NSEnumerator *matchEnumerator = NULL; NSString *regex = @"<img[^>]+alt=/"([^>]+)/"[^>]*>"; matchEnumerator = [searchString matchEnumeratorWithRegex:regex]; NSUInteger line = 0; NSString *matchedString = NULL; while((matchedString = [matchEnumerator nextObject]) != NULL) { NSString *imgTag = matchedString; NSMutableString *alt = [NSMutableString stringWithString:imgTag]; NSString *replaceWithString = @"$1"; NSUInteger replacedCount = [alt replaceOccurrencesOfRegex:regex withString:replaceWithString]; if (replacedCount) { NSString *abbr = [abbreviationMappings objectForKey:alt]; if (!abbr) { abbr = [NSString stringWithFormat:@"[%@]", alt]; } searchString = [searchString stringByReplacingOccurrencesOfString:imgTag withString:abbr]; } line++; } program.details = searchString; } |
置换字符串
1 2 3 4 5 6 7 | NSString *result; NSString *sample = @"Phone Num : 010-123-456-789"; NSString *regex = @"(//d{3})-"; NSString *replace = @"$1,"; result = [sample stringByReplacingOccurrencesOfRegex:regex withString:replace]; NSLog(@"replace: %@", result); |
如上所示的例子,数字间的“-”被置换为“,”输出结果为:
1 | replace: Phone Num : 010,123,456,789 |
分割字符串
1 2 3 4 | NSString *sample = @"This is sample"; NSString *regex = @"//s+"; NSArray *results = [sample componentsSeparatedByRegex:regex]; NSLog(@"results: %@", results); |
结果如下:
1 2 3 4 5 | results: ( This, is, sample ) |
除此之外,还有许多实用的地方,有兴趣的可以继续研究。