Can you use regex inside .query string matches, trying to do word match search, for example:
final regexText = "/\b($searchText)\b/i";
final items = realm.query<Post>(
r"post CONTAINS[c] $0 SORT(createdAt DESC)", ["regexText"]);
… but doesn’t seem to work.
No, regex is not supported. There’s a
LIKE operator that supports
? matches, but no true regex capabilities. You can read more about the query language in the docs.
Thanks for getting back. Any plans to support this soon? @nirinchev
I’m not aware of any short-term plans to support it, unfortunately
A work-around could be to do the matching in Dart. As realm is an embedded database the cost is often acceptable.
final re = RegExp(...);
Iterable<Post> matches = realm.all<Post>().where((p) => re.hasMatch(p.post));
Note that you no longer benefit from any indexes. As the number of posts grow the above will eventually become expensive - it is after all a linear scan - but 10k posts is unlikely to be a problem.
Yeah, that’s a good suggestion as well @Kasper_Nielsen1 . Or, get the query results from Realm, and then parse those results in Dart for word match. THanks!