Java Regex pattern matching
I am developing a text highlighter in a file using the java regex pattern matching. Following is a code snapshot of it
SearchQuery=preproce开发者_如何转开发ssedModifiedArrayList.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
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 +).
精彩评论