iPhone开发技巧之数据篇(1)--- 使用正则表达式
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 03:39 点击:次
在处理字符串的时候,常常会用到正则表达式,在iphone os上也不例外。使用 RegexKit Frameworkhttp://regexkit.sourceforge.net/RegexKitLite/index.html
解压 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.&n
就可以了。在这里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.&n
- 发表评论
-
- 最新评论 更多>>