1

I feel like the answer to my question might already be somewhere on this forum, but after hours of scouring I just can't quite seem to get it right. I've never used SQL before so please bear with me.

I have a core data model with only one entity and that entity has an attribute called "definitions" (each entity in the database is a dictionary entry). What I want to do is search the definitions for an input string with word boundaries. If I do a simple CONTAINS[cd] search with NSPredicate and the input text I get what I'm looking for, except that I also get definitions that contain the input sequence of characters, regardless of boundaries.

So I've also tried building a regex string like @"(?w).*\\btheWord\\b.*", along with the MATCHES[cd] search command, but I read on other posts that can only be used when searching NSStrings, and regardless it didn't work.

This should be simple but I'm stumped. Any help appreciated.

EDIT : Here's an example of what I'm doing

YTAppDelegate * delegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext * context = delegate.managedObjectContext;

NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"YTDictData"];


NSString * searchTerm = [NSString stringWithFormat:@"'(?w).*\\b%@\\b.*'",[NSRegularExpression escapedPatternForString:inputText]];

NSString * searchString = [NSString stringWithFormat:@"definitions contains[cd] %@",searchTerm];

request.predicate = [NSPredicate predicateWithFormat:searchString];

NSError * searchError = nil;

NSArray * searchResults = [context executeFetchRequest:request error:&searchError];

In this case, if I were to search for the word "hello", I would get no matches.

However if I do not format the searchString string for regex searching (ie searchString = inputString), I will get any definition that contains the sequence of characters "hello". I need to be able to find strings which contain the string "hello", but I also need the added constraint that I want "hello" to be recognized as a word with boundaries.

themantalope
  • 1,040
  • 11
  • 42
  • Show your original attempt and explain what it returns (that is right and wrong). – Wain Jan 23 '15 at 07:41
  • Hi @Wain thanks for your suggestion. Please take a look at the edits. – themantalope Jan 23 '15 at 17:17
  • Have you tried using `MATCHES` for the regex match? See Apple's "Predicate Programming Guide". – Tom Harrington Jan 23 '15 at 17:55
  • @TomHarrington yes I have. The `MATCHES` search also returns no results. Is special formatting required for a `MATCHES` command? Can you use `MATCHES` when searching a core data database? From my understanding the regex searching only works when searching NSStrings that are already in memory. I would prefer to take advantage of the SQL interfacing that core data has built into it. – themantalope Jan 23 '15 at 18:03
  • According to this answer: http://stackoverflow.com/a/2885731/2128900 it's possible to use regex in a fetch request's predicate. Maybe the syntax which you use doesn't work with SQLite store. – Michał Ciuba Jan 23 '15 at 18:47

1 Answers1

4

As already said in the comments, you have to use the "MATCHES" operator for regular expression matching. In addition, you should not use stringWithFormat to build the predicate string (your searchString variable).

This is how it should work:

NSString *searchTerm = [NSString stringWithFormat:@".*\\b%@\\b.*",
            [NSRegularExpression escapedPatternForString:inputText]];
request.predicate = [NSPredicate predicateWithFormat:@"definitions MATCHES[cd] %@",
                                    searchTerm];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks for pointing out the nuance about using another string directly in the `[NSPredicate predicateWithFormat:]` method - beforehand I had to insert `'` marks, otherwise I would get a SQL error. However those `'` marks were probably being incorporated into the search parameter, which is why I wasn't finding anything. – themantalope Jan 23 '15 at 22:24
  • @Matt: You are welcome. – The main problem in your method is actually the "backslash-b". In `[NSPredicate predicateWithFormat:searchString]` this becomes a backspace character. – Martin R Jan 23 '15 at 22:30