使用这个命令行示例应用程序它工作得很好。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *lines = @[ @"17% off etc.", @"2% off", @"off by 12%" ];
NSPredicate* test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0-9]{1,2}% off.*"];
[lines enumerateObjectsUsingBlock:^(NSString *candidate, NSUInteger idx, BOOL *stop) {
if ([test evaluateWithObject:candidate]){
NSLog(@":)");
} else{
NSLog(@":(");
}
}];
}
return 0;
}
输出:
2012-08-08 15:46:16.213 regextest[1305:303] :)
2012-08-08 15:46:16.215 regextest[1305:303] :)
2012-08-08 15:46:16.215 regextest[1305:303] :(
如果行包含多个字符,它应该看起来像
NSPredicate* test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0-9]{1,2}% off.*"];
的.*
表明,任何数量的任何字符的可能存在。
我没有问题,可以选择“23%off”或“7%off”。你的候选人的形状是否合适? – Matthias 2012-08-08 13:41:14
是的..我确信他们以数字开头。 – Zsolt 2012-08-08 13:42:29