Search code examples
iosobjective-cnsarraynspredicatenscompoundpredicate

NSPredicate for filter objects whoes string is BEGINSWITH special characters


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]+.*"];

Solution

  • 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]