iPhone开发技巧之数据篇(1)--- 使用正则表达式

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 03:39 点击:
在处理字符串的时候,常常会用到正则表达式,在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.&n
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • 如何为Iphone应用创建启动界面
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1