Java – dangling metacharacter
•
Java
When I use ", '*', '(' and ')', I always receive errors about hanging metacharacters
I've tried to escape those characters in regular expressions, but I still get errors This is what I have:
"[-\\+\\*/%\\(\\)]"
to update:
Test:
String input = "+"; String vals = new WNScanner(input).getNextToken(); //********** System.out.println("token: " + vals); System.out.println(vals.matches("[-+*/%()]"));
From another class:
... String expression = input; ... public String getNextToken() { String[] token = {""}; if (expression.length() == 0) return ""; token = expression.split("\\s"); recentToken = token[0]; expression = expression.replaceFirst(token[0],""); //************* expression = expression.trim(); return token[0]; }
*There are exceptions to these lines
Solution
Well, I don't know what you want to achieve there... Especially in this business:
expression = expression.replaceFirst(token[0],"");
If your input string is "", then your entire regular expression is "" This is illegal
You need to reference the input string to use it in any regular expression - related operation, including string Replacefirst() and Replaceall() (excluding. Replace()...)
Therefore, do the following:
final String re = Pattern.quote(token[0]); expression = expression.replaceFirst(re,"");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码