NSPredicate and regular expressions
Andy on Sep 29th 2006
I’ve managed to figure out how to implement wildmat patterns inside of Wombat. It turns out NSPredicate does in fact support regular expressions, as documented in Using Predicates. You simply use the MATCHES operator to specify a regular expression, as shown in a couple of examples in Apple’s documentation. It’s implemented using ICU’s Regular Expressions package, which provides much better documentation than Apple.
The gotcha to note is that the MATCHES operator does not compile down to SQL, so you can’t give it directly to Core Data. Instead you have to pull out all the entities then post-process the array using NSArray’s filteredArrayUsingPredicate: method. It works, but it’s not as efficient as it would be if it were compiled down to SQL.
In a related news, Mike Zornek was kind enough to point out a small Core Data mailing list. Hopefully it will be a source of useful information in the future.
Filed in Core Data, Macintosh, Programming | One response so far

Melissa Turner Nov 3rd 2006 at 04:28 pm 1
Regex isn’t supported by the SQL standard, but SQLite allows you to register custom functions, and Core Data registers for LIKE and MATCHES to get the same matching behavior you see with NSPredicate.
The performance characteristics of the matching logic are the same between the implementations, but you lose all the overhead of realizing objects so you can discard them.