mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
446 stars 98 forks source link

MongoDart Error: Regular expression is invalid: missing terminating ] for character class #349

Closed SittiphanSittisak closed 7 months ago

SittiphanSittisak commented 12 months ago

I get this error when use it.

mongodb.dbCollection.find(where.match('name', '[')) // any words that contain '['
giorgiofran commented 12 months ago

Try "\[". Match uses a regular expression. "[" is a special character an needs to be escaped

SittiphanSittisak commented 12 months ago

Is there another special character in mongo_dart that I should know? I can use a sub string every time I use match but is possible to make this handle on this package?

giorgiofran commented 12 months ago

This has nothing to do with mongo_dart. It is the Regular Expression syntax. Search in internet the documentation or give a look to the RegExpr class in dart.

SittiphanSittisak commented 12 months ago

Thank you, I got it.

Update using this before using match

RegExp.escape(keyWord)
SittiphanSittisak commented 11 months ago

Hi, for this issue I must use RegExp.escape for every keyword that is used for the .match method. If not use this and the keyword contains a character, it will always error. So, is it possible to add using the RegExp.escape in the .match method?

like this

  SelectorBuilder match(String fieldName, String pattern,
      {bool? multiLine, bool? caseInsensitive, bool? dotAll, bool? extended}) {
    _addExpression(fieldName, {
      '\$regex': BsonRegexp(RegExp.escape(pattern),
          multiLine: multiLine,
          caseInsensitive: caseInsensitive,
          dotAll: dotAll,
          extended: extended)
    });
    return this;
  }
giorgiofran commented 11 months ago

I have added a new parameter that does what you want. You have to updat the dependencies so that you will use the last version of the mongo_dart_query package.

giorgiofran commented 7 months ago

I close this issue, feel free to reopen it if needed.