Cannot get Couchbase Lite Regex query to work

I have a simple document structure where a number of locations of different types are held (with GPS location and name).
An example:

{
  "ispublic": true,
  "key": "8641",
  "name": "Grærup Langsø",
  "points": [
    {
      "lat": 55.6529802987,
      "lon": 8.1668484987
    },
    {
      "lat": 55.6563176391,
      "lon": 8.167440034
    },
    {
      "lat": 55.6518253871,
      "lon": 8.167535358
    }
  ],
  "type": "Lake"
}

If the user has not allowed the app to use the GPS then the user needs to search for the location (where he is fishing). So I have created this condition:

_condition = Expression.Property("name").Regex(Expression.String(search + ".*/i"))
    .And(Expression.Property(TYPE).EqualTo(Expression.String(typeof(Lake).Name))
        .Or(Expression.Property(TYPE).EqualTo(Expression.String(typeof(PutTakeLake).Name)))
        .Or(Expression.Property(TYPE).EqualTo(Expression.String(typeof(CoastLocalArea).Name)))
        .Or(Expression.Property(TYPE).EqualTo(Expression.String(typeof(SeaLocalArea).Name)))
        .Or(Expression.Property(TYPE).EqualTo(Expression.String(typeof(Stream).Name)))
        .Or(Expression.Property(TYPE).EqualTo(Expression.String(typeof(Country).Name))));

It includes all types of locations and tries to find the ones with a name that start with the letters entered by the user. But it does not match any names…

If I use

Expression.Property("name").Like(Expression.String(search + "%"))

Then it does match if I use the exact case “Gr” - but not “gr” (which it actually should according to the description and examples in: https://docs.couchbase.com/couchbase-lite/2.6/csharp.html#wildcard-match).

I have tried all sorts of different syntax attempts with the regex - but to no avail. I need to add the “/i” option to perform a case insensitive search. Attempts I have tried:

"gr/i"
"gr.*/i"
"/gr/i"
"gr\\/i"
"gr\\i"

And more (just pure wild shooting)…
Can anyone direct me to the right syntax

Ok, found out that the documentation is wrong. According to this post (that I did not find at first) Like is now case sensitive…

Using the suggestion to make all lower case seems to work.

Then I don’t need the Regex function - but would still be nice to know how the syntax should be when transforming from “standard regex” :wink:

We don’t have an API to pass through “regex flags” unfortunately. The regex will be applied via the C++11 regex class with the regex_constants::ECMAScript flag (Documentation on what that means can be found here. It does not accept flags on the end of the regex pattern.

1 Like

OK, thanks, Jim.

/John

We should probably improve the regex support to take into account the current collation. Then you can use the “collate” functionality in queries to control regex matching.

Well, it kind of fooled me… - but I guess if the case sensitivity issue in the doc for Likeis corrected then I wouldn’t even have looked at the Regex function :slight_smile:

Sorry about the incorrect documentation. We have a ticket filed to get it fixed.

1 Like