我想写一个简单的正则表达式来检查给定字符串中是否存在任何特殊字符。我的正则表达式工作,但我不知道为什么它也包括所有的数字,所以当我把一些数字它返回一个错误。
我的代码:
//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!");
}