I am trying to match a URL pattern but it’s nothing match. Basically, anything from yahoo is allowed, so for example ca.yahoo.com is allowed. It works for that, but it won’t work for simply yahoo.com. Here’s my code:
NSString *string = @"http://yahoo.com/";
NSString *regex = @"^http://.*\\.yahoo.com/.*$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[C] %@", regex];
BOOL match = [predicate evaluateWithObject:string];
NSLog(@"%@", match? @"MATCH" : @"NO LUCK");
prints NO LUCK
The problem is that the period just prior to yahoo is not optional. Here is a version that makes the entire preceding optional:
Here is a simpler way using
rangeOfString::NSLog output: