我想写一个简单的正则表达式来检查给定字符串中是否存在任何特殊字符。我的正则表达式可以工作,但我不知道为什么它还包括所有数字,所以当我输入一些数字时它会返回错误。
我的代码:
//pattern to find if there is any special character in string
Pattern regex = Pattern.compile("[$&+,:;=?@#|'<>.-^*()%!]");
//matcher to find if there is any special character in string
Matcher matcher = regex.matcher(searchQuery.getSearchFor());
if(matcher.find())
{
errors.rejectValue("searchFor", "wrong_pattern.SearchQuery.searchForSpecialCharacters","Special characters are not allowed!");
}
[]
中的短横线应该进行转义,因为在那里它有特殊的意义。 - MightyPork