1

I am trying to run a query against a mongodb database. The user query is a regular expression in a format similar to perl. I'm translating the user's regex to Mongo filter.

Here is the query code:

private List<string> GetDocuments(string by_regex)
{
    string regex = by_regex.Replace("[", @"\[");
    regex = regex.Replace("]", @"\]");
    regex = regex.Replace("*", ".*");
    regex = "^" + regex + "$";

    var filter = string.Format("{{_id:'CRF^{0}'}}", regex);
    MyObject item = collection.Find(filter).SingleOrDefault(); 
    ....
}

Invoking the above method with the regular expression *.crc is throwing an exception at the Find statement:

Invalid escape sequence in JSON string '\.'.

The filter at runtime is {_id:'CRF^^.*\\.crc$'} - so I assume this is something with the . char escaping, but for some reason I can't find the right way to escape it for Mongo to not complaint.

NirMH
  • 4,769
  • 3
  • 44
  • 69
  • 2
    Try scaping `.` as `[.]` to see if that does the trick – Julio Jul 12 '18 at 07:51
  • @Julio: Thanks - this did the trick... – NirMH Jul 12 '18 at 11:09
  • Great! I'm adding it as an answer – Julio Jul 12 '18 at 11:36
  • I think this is a case of the .NET driver for Mongo not accepting it, because when I use a MongoDB client (in my case, Robo 3T), it accepts `\.` just fine as a part of a `$regex:` value. Or maybe Robo is smarter than the average bear and is translating it on the fly? *shrug* – Mike Loux Jul 20 '21 at 15:15

2 Answers2

1

You have to escape dot in your regexp like this:

db.Test.find({filename: { $regex: '.*\\.crc', $options: 'i'}})
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
1

According to the error message, MongoDB seems to try to decode JSON string like \., and that is not a valid scape sequence.

See: https://www.freeformatter.com/json-escape.html

So perhaps you shoud change . to \\\\. so that will be encoded as a real \\ that will be decoded in JSON as \

Alternativelly, as I said in my comment, sometimes It is way much easier to use classes for scaping characters. So you may use the [.]. That way you avoid the use of backslashes, which are special characters too and they may need to be scaped several times depending of whether the regular expression is a string or not.

Julio
  • 5,208
  • 1
  • 13
  • 42