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
)

除此之外,还有许多实用的地方,有兴趣的可以继续研究。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值