开发者

Java Regex pattern matching

开发者 https://www.devze.com 2023-03-30 07:29 出处:网络
I am developing a text highlighter in a file using the java regex pattern matching. Following is a code snapshot of it

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 +).

0

精彩评论

暂无评论...
验证码 换一张
取 消