Reputation: 139
I have field in mongoDb collection "name" which contains:
"26.11.2018(2)"
I use regex expression for searching if any string contains in field "name":
String search = "11.2018(2)";
return Criteria.where("name").regex(search);
I got exception, that regular expression is wrong(because of ")"). Is there any other possibility for searching like this?
Upvotes: 3
Views: 6048
Reputation: 23307
You need to escape the value that's used in the regex.
I can't test it now, but it's possible that it may work:
String search = "some pattern(a)12.";
pattern = Pattern.compile(Pattern.quote(search), Pattern.CASE_INSENSITIVE);
return Criteria.where("name").regex(pattern);
https://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/client/model/Filters.html
Upvotes: 3