【发布时间】:2012-07-07 15:07:24
【问题描述】:
我今天正在修改 Objective-C,但我遇到了一些奇怪的行为。基本上我试图从 NSString 替换所有非字母小写字符。我基本上归结为:
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSError *error;
NSRegularExpression *pattern = [[NSRegularExpression alloc] initWithPattern:@"/[^abcdefghijklmnopqrstuvwxyz]/" options:0 error:&error];
NSString *replacableStuff = @"a b c\nd e";
NSLog(@"%@", [pattern stringByReplacingMatchesInString:replacableStuff options:0 range:NSMakeRange(0, [replacableStuff length]) withTemplate:@""]);
}
return 0;
}
但是,替换似乎永远不会发生;运行此日志“a b c\nd e”到日志事物。 (我期待看到“abcde”。)我尝试了更简单的模式,例如/[aeiou]/,甚至只是/a/,但无论我尝试什么,stringByReplacingMatchesInString 方法似乎并没有真正取代任何东西。我忽略了什么?
【问题讨论】:
标签: objective-c regex cocoa nsregularexpression