在處理字串的時候,常常會用到Regex,在iphone os上也不例外。使用 RegexKit Frameworkhttp://regexkit.sourceforge.net/RegexKitLite/index.html 就可以了。在這裡http://downloads.sourceforge.net/regexkit/RegexKitLite-4.0.tar.bz2 下載RegexKitLite。
解壓 RegexKitLite-4.0.tar.bz2 :
1. RegexKitLite.h
2. RegexKitLite.m
3. RegexKitLite.html
4. examples
5. RKLMatchEnumerator.h
6. RKLMatchEnumerator.m
7. NSString-HexConversion.h
8. NSString-HexConversion.m
9. link_example.m
10. main.m
使用這裡,我們只需要 RegexKitLite.h 和 RegexKitLite.m 兩個檔案,將其加入到你的工程中。另外加入 -licucore 連結開關。簡單的例子如下:
1. NSString *searchString = @"This is neat.";
2. NSString *regexString = @"\\b(\\w+)\\b";
3. NSString *replaceWithString = @"{$1}";
4. NSString *replacedString = NULL;
5.
6. replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
7. NSLog(@"replaced string: '%@'", replacedString);
出結果為:
1. replaced string: '{This} {is} {neat}.'
時,也可以使用 Enumerator 來取得每個匹配的項。
1. #import <Foundation/NSAutoreleasePool.h>
2. #import "RegexKitLite.h"
3. #import "RKLMatchEnumerator.h"
4.
5. int main(int argc, char *argv[]) {
6. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
7.
8. NSString *searchString = @"one\ntwo\n\nfour\n";
9. NSEnumerator *matchEnumerator = NULL;
10. NSString *regexString = @"(?m)^.*[ DISCUZ_CODE_3 ]quot;;
11.
12. NSLog(@"searchString: '%@'", searchString);
13. NSLog(@"regexString : '%@'", regexString);
14.
15. matchEnumerator = [searchString matchEnumeratorWithRegex:regexString];
16.
17. NSUInteger line = 0;
18. NSString *matchedString = NULL;
19.
20. while((matchedString = [matchEnumerator nextObject]) != NULL) {
21. NSLog(@"%d: %d '%@'", ++line, [matchedString length], matchedString);
22. }
23.
24. [pool release];
25. return(0);
26. }
例子 解析HTML下面用一個例子,來舉例匹配HTML中字串的方法。從img-tag中抽出alt屬性的值。
1. <img src="/img/icon_new_b.gif" alt="test1" width="13" height="13" />
2. <img src="/img/icon_news_b.gif" alt="test2" width="13" height="13" />
1. NSString *details = [item objectForKey:@"description"];
2. if ([details length] > 0) {
3. NSString *searchString = [details stringByHalfwideningLatinCharacters];
4.
5. NSEnumerator *matchEnumerator = NULL;
6. NSString *regex = @"<img[^>]+alt=\"([^>]+)\"[^>]*>";
7. matchEnumerator = [searchString matchEnumeratorWithRegex:regex];
8. NSUInteger line = 0;
9. NSString *matchedString = NULL;
10. while((matchedString = [matchEnumerator nextObject]) != NULL) {
11. NSString *imgTag = matchedString;
12. NSMutableString *alt = [NSMutableString stringWithString:imgTag];
13.
14. NSString *replaceWithString = @"$1";
15. NSUInteger replacedCount = [alt replaceOccurrencesOfRegex:regex withString:replaceWithString];
16. if (replacedCount) {
17. NSString *abbr = [abbreviationMappings objectForKey:alt];
18. if (!abbr) {
19. abbr = [NSString stringWithFormat:@"[%@]", alt];
20. }
21. searchString = [searchString stringByReplacingOccurrencesOfString:imgTag withString:abbr];
22. }
23. line++;
24. }
25. program.details = searchString;
26. }
變換字串
1. NSString *result;
2. NSString *sample = @"Phone Num : 010-123-456-789";
3. NSString *regex = @"(\\d{3})-";
4. NSString *replace = @"$1,";
5.
6. result = [sample stringByReplacingOccurrencesOfRegex:regex withString:replace];
7. NSLog(@"replace: %@", result);
如上所示的例子,數字間的“-”被置換為“,”輸出結果為:
1. replace: Phone Num : 010,123,456,789
分割字串
1. NSString *sample = @"This is sample";
2. NSString *regex = @"\\s+";
3. NSArray *results = [sample componentsSeparatedByRegex:regex];
4. NSLog(@"results: %@", results);
結果如下:
1. results: (
2. This,
3. is,
4. sample
5. )
除此之外,還有許多實用的地方,有興趣的可以繼續研究。
摘自 ioser