Regularly, escape characters are required
'$', '(', ')', '*', '+', '.', '[', ']', '?', '//', '^', '{', '}', '|'
Anomaly:
java.util.regex.PatternSyntaxException: Dangling meta. character '*' near index 0
Solution
Just add // to special characters.
Note: Although using [] is also possible under some conditions, if the range boundary start symbols do not match (, [, {, etc., it will be reported as follows:
Anomaly
java.util.regex.PatternSyntaxException: Illegal repetition near index 50
The Java filtering regular expression special word code is as follows ( Note: //The first replacement is required, otherwise there will be logical bugs when replacing the replace method)
/** * Escape regular special characters ($()*+.[]?/^{},|) * * @param keyword * @return */public static String escapeExprSpecialWord(String keyword) { if (StringUtils.isNotBlank(keyword)) { String[] fbsArr = { "//", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" }; for (String key : fbsArr) { if (keyword.contains(key)) { keyword = keyword.replace(key, "//" + key); } } } return keyword;}Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.