包含所有特殊字符的正则表达式模式

2022-08-31 09:39:44

我想写一个简单的正则表达式来检查给定字符串中是否存在任何特殊字符。我的正则表达式工作正常,但我不知道为什么它也包括所有数字,所以当我输入一些数字时,它会返回一个错误。

我的代码:

//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!");
}

答案 1

请不要这样做...小Unicode BABY ANGELs 像这样


答案 2