1

I have one scenario where I want to filter the objects of NSArray who have NSDictionary object and every object have key name. I want to filter with that number of objects whose name key value should be starting with special characters or numeric characters like 0-9,~!@#$%^&*()_ etc.

I have try to find this on google but not getting proper solution. I have used below predicate but not able to getting right objects.

NSPredicate *predicate=   [NSPredicate predicateWithFormat:@"self.name BEGINSWITH %@",@"[^0-9]+.*"];
Ravi B
  • 1,574
  • 2
  • 14
  • 31

1 Answers1

1

The BEGINSWITH operator doesn't support regular expressions. You can check if it begins with Non Alphabetic character using following in predicate:

 NSString *myRegex = @"[A-Za-z]*";
 NSPredicate *myTestPred = [NSPredicate predicateWithFormat:@"NOT (SELF MATCHES %@)", myRegex]
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
  • not working.. it's crashing with exception on run time. `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "NOT (SELF MATCHES[c] '^[A-Za-z].*'"'` – Ravi B Dec 27 '16 at 07:48
  • I have tried after solving syntax error in predicate but still not working – Ravi B Dec 27 '16 at 08:05
  • Yes its working now, but I think `regex` should be this `@"^[A-Za-z]*"`.. but its ok its working.. Thanks once again. – Ravi B Dec 27 '16 at 08:33