I would like to search if in the attribute description (an NSString instance) there is a given word.
I tried with this predicate:
[NSPredicate predicateWithFormat:@"description CONTAINS[cd] %@", theWord];
It works, but it finds also sub-words.
For example, given theWord:
Car
it will mach also this description:
A Christmas Carol
Instead, I would like that my predicate will match only a, or christmas, or carol.
(I’m using Core Data and NSFetchRequest.)
Something like this?
The trick is the
\bmetacharacter, which denotes a word boundary in the string. In order to get a backslash “into” the regex pattern string, you have to precede it with another backslash so the compiler understands that there should be a real backslash in the string. Hence the “\\b” in the string.Also, in order to cover non-English language strings better, you should enable Unicode word boundary detection, by setting the
wflag option. The match string will look like this: