0

This Regex is working in mongo query as below :

{$match : {'file_details.tec_stop' : {$regex : /(\d\|){1}\d/}}},

but now working when using java :

String stopRegex="/(\\d\\|){"+stops+"}\\d/";
pipeline.add(new BasicDBObject("$match", new BasicDBObject("file_details.tec_stop'", new BasicDBObject("$regex", stopRegex))));

What's the issue?

2
  • You don't delimit a Java regex within a String using '/'. That would be redundant. Commented Mar 24, 2017 at 7:36
  • I'd also call Pattern.quote(stops) and use the result in stopRegex. Commented Mar 24, 2017 at 7:39

1 Answer 1

1

You need to remove the first / and the last / from your Java regex. They are Javascript specific regex literal which is the language used by mongodb shell

Javascript RegExp

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

2 Comments

Na it's still not working either. I have a string like 1|0|2 and I am trying to match it with below reqular expression in java ^(\d\|){2}\d$
Because | is a special character in regex, you need to escape it first by prepending it with a backslash like \|, so to match a digit|a digit|a digit you need ^\\d\|\\d\|\\d$

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.