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.

4
  • 2
    Try scaping . as [.] to see if that does the trick Commented Jul 12, 2018 at 7:51
  • @Julio: Thanks - this did the trick... Commented Jul 12, 2018 at 11:09
  • Great! I'm adding it as an answer Commented Jul 12, 2018 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 Commented Jul 20, 2021 at 15:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to escape dot in your regexp like this:

db.Test.find({filename: { $regex: '.*\\.crc', $options: 'i'}})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.