3

I am developing a text highlighter in a file using the java regex pattern matching. Following is a code snapshot of it

SearchQuery=preprocessedModifiedArrayList.get(i)+"[\\w\\s\\W]*?";
pattern = Pattern.compile(SearchQuery);
Matcher matcher = pattern.matcher(EXAMPLE_TEST);

in here "preprocessedModifiedArrayList.get(i)" contains the query to be searched in the text of the file. I have a problem that when the "preprocessedModifiedArrayList.get(i)" has "+" sign in it(example: if it is an equation) , it returns the dangling + exception.

I want to know how can I deal with this problem

0

1 Answer 1

7

You may quote it:

SearchQuery=Pattern.quote(preprocessedModifiedArrayList.get(i))+"[\\w\\s\\W]*?";

Quoting will escape every special character in the pattern so that they behave as normal characters (like +).

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

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.