4六/100
iPhone开发技巧之数据篇(1)— 使用正则表达式
iPhone开发技巧之数据篇(1)--- 使用正则表达式
在处理字符串的时候,常常会用到正则表达式,在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 ) |
除此之外,还有许多实用的地方,有兴趣的可以继续研究。
相关文章
- iPhone开发技巧之发布篇(7)--- 制作自己的Cydia发布源 - (2012-01-20)
- iPhone开发技巧之发布篇(6)--- 不需Developper认证的真机调试方法 - (2011-12-25)
- iPhone开发技巧之环境篇(11) --- 让Xcode对应多个版本的iOS SDK - (2011-12-03)
- iPhone开发技巧之发布篇(5)--- 在程序中添加广告 - (2011-11-20)
- iPhone开发技巧之环境篇(10)--- 在控制台调试iPhone应用程序 - (2011-11-13)
- iPhone开发技巧之调试篇(3)--- 程序Crash后的调试技巧 - (2011-11-06)
- iPhone开发技巧之环境篇(9)--- Xcode中的注释 - (2011-01-12)
- iPhone开发技巧之数据篇(2)--- iPhone程序中的加密处理 - (2011-01-10)
- iPhone开发技巧之发布篇(4)--- 使用 Ad Hoc 发布自己的应用程序 - (2010-07-22)
- iPhone开发技巧之发布篇(3)--- 你的程序被拒了吗? - (2010-07-19)
- iPhone开发技巧之发布篇(2)--- 税务相关手续 - (2010-07-16)
- iPhone开发技巧之发布篇(1)--- 登录银行信息 - (2010-07-12)
- iPhone开发技巧之工具篇(4)--- 使用afconvert转换WAV文件 - (2010-06-24)
- iPhone开发技巧之私有API(8)--- UIApplication - (2010-06-18)
- iPhone开发技巧之私有API(7)--- 用UIWebView访问BASIC认证的页面 - (2010-06-16)
- iPhone开发技巧之私有API(6)--- 设置UIWebView中的User-Agent - (2010-06-14)
- iPhone开发技巧之私有API(5)--- UISegmentedControl - (2010-06-11)
- iPhone开发技巧之私有API(4)--- UIBarButtonItem - (2010-06-09)
- iPhone开发技巧之私有API(3)--- UIButton - (2010-06-07)
- iPhone开发技巧之私有API(2)--- UITableView - (2010-06-01)
- iPhone开发技巧之私有API(1) --- 设备相关信息 - (2010-05-28)
- iPhone开发技巧之网络篇(5)--- 使用libcurl连接https服务器 - (2010-05-17)
- iPhone开发技巧之网络篇(4)--- 确认网络环境 3G/WIFI - (2010-05-14)
- iPhone开发技巧之网络篇(3)--- 使用NSOperation建立多任务网络连接 - (2010-05-12)
- iPhone开发技巧之网络篇(2)--- Web服务 - (2010-04-20)