manuelBetancurt
manuelBetancurt

Reputation: 16138

ios core data predicate search for word similar

Im using coredata in my app,

I can search all entities with a starting letter,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name MATCHES '^[hH].*'"];
//sort descriptor!
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *companyProducts = [Company allWithPredicate:predicate sortDescriptors:@[sortDescriptor]];
Company *theCompany;
NSLog(@"tus companies number:: %d", companyProducts.count);
for (theCompany in companyProducts) {
    NSLog(@"tus companies:: %@", theCompany.name);
}

so in this case I will get all companies starting with h no matter lower or upper case...

but if I need to search for matches with more than one letter? no matter lower or upper case,

for example to look for:

Hyper hyosung

So I need to know how to construct my regex? to have a search for n number of charaters on my properties?

Thanks!

Upvotes: 0

Views: 373

Answers (1)

Anupdas
Anupdas

Reputation: 10201

Try with

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH [c] %@",@"H"];

Upvotes: 4

Related Questions