Reputation: 3760
I need the regex formula for ex "17% off etc etc" or "2% off etc etc etc" so if the string starts with a one or 2 digit number followed by the percent sign, space, and the word 'off'
I tried
NSPredicate* test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0-9]{1,2}% off"];
if ( [test evaluateWithObject:candidate] )
{
.... etc..
but no luck.
I appreciate any insight.
Upvotes: 1
Views: 324
Reputation: 52227
With this command line example app it works just fine.
#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;
}
output:
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] :(
If the line contains more chars, it should look like
NSPredicate* test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0-9]{1,2}% off.*"];
the .*
indicates, that any number of any char might be present.
Upvotes: 1