iPhone正则表达式捕获组

时间:2010-06-26 19:56:49

标签: iphone regex ios4

由于Apple现在似乎拒绝链接到libicucore的应用程序(例如使用RegexKitLite的应用程序),在iPhone应用程序中使用捕获组功能获得正则表达式的最佳方法是什么?是否应该静态编译ICU或PCRE(可能使用RegexKit PCRE包装器)?

2 个答案:

答案 0 :(得分:1)

有关导致使用RegexKitLite的应用程序被拒绝的原因的详细信息,请参阅this cocoa-dev post

简短的回答是仍然可以使用RegexKitLite而 会被拒绝。

答案 1 :(得分:0)

我通过NSRegularExpression获得第一组,将命中数替换为第一组:

的字符串= “ABCDE” 图案= “B(C)”。 =>命中= “BCD” =>基团= “C”

NSRegularExpression* regex=[NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
    NSRange range=[regex rangeOfFirstMatchInString:self  options:0 range:NSMakeRange(0, [self length])];
    String hit=[self substringWithRange:range];
    return [regex stringByReplacingMatchesInString:hit options:0 range:NSMakeRange(0, [hit length]) withTemplate:@"$1"];
相关问题