I have a string say, "0,1,2,3,4,5,6,7,8,9,10,11"
(Assumed to be months index string) in my Core Data. I want to use a predicate to fetch whether that field contains a number say, string contains '0'.
We cannot use 'CONTAINS' since '0' is also present in '10'. I need to fetch the object using NSPredicate
to avoid loops from Core Data.
Update:
I just want to test whether '0,1,2,3,4,5,6,7,8,9,10,11' contains '0' or not using NSPredicate
.
Solved
The "MATCHES" operator of NSPredicate
can be used to compare against a regular expression:
NSString *searchTerm = @"0";
NSString *regex = [NSString stringWithFormat:@"(.*,)?%@(,.*)?", searchTerm];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"strIndex MATCHES %@", regex];
This worked fine for me. Thank you @skagedal for the suggestion. I got this answer from Form NSPredicate from string that contains id's.