我使用 preg_split
根据 PHP 中的正则表达式分割字符串,使用以下代码:
$array = preg_split("~(?<!\*),~", $string);
cocoa 中的等价物是什么?
感谢任何帮助。
最佳答案
最终使用 NSRegularExpression
编写了我自己的方法
+ (NSArray *)preg_split:(NSString *)expression withSubject:(NSString *)subject {
NSRegularExpression *exp = [NSRegularExpression regularExpressionWithPattern:expression options:0 error:nil];
NSArray *matches = [exp matchesInString:subject options:0 range:NSMakeRange(0, [subject length])];
NSMutableArray *results = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in matches) {
[results addObject:[subject substringWithRange:[match range]]];
}
return results;
}
关于regex - cocoa preg_split 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14807364/