0
    String pattern = ".*" + "a? I'm" + ".*";
    FindIterable<Document> document = collection.find(regex("mypost", pattern, "i")).sort(new Document("mypost", -1));

I want a regex that contains the terms "a? I'm". For some reason this pattern picks up collections with mypost as "? I'm" when I want only "a? I'm".

What is wrong with it?

1 Answer 1

1

The problem with your regex is that ? has special meaning

? - Once or none

So your regex is basically saying - before " I'm" you can have a or not.

Your regex in fact should look like this:

String pattern = ".*" + "a\\? I'm" + ".*";

By adding \\ you should specify that you want to use ? as a character.

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

4 Comments

Okay that does it fix but now my issue is that I cant manually change every symbol with 2 `\`'s since that part of the text isn't always fixed if that makes sense. Is there a command like exactText(string)
\\ Won't show as a character, why don't you experiment?
@FailingCoder I mean if I were send a request with a string such as "abc? I'm" RESTfully I wont know the position of every symbol
Experiment then.

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.