iPhone开发技巧之数据篇(1)--- 使用正则表达式
在处理字符串的时候,常常会用到正则表达式,在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
补充:移动开发 , IOS ,