我想编写一个简单的正则表达式来检查给定字符串中是否存在任何特殊字符。我的正则表达式工作,但我不知道为什么它也包含所有数字,所以当我把一些数字返回错误。包含所有特殊字符的正则表达式模式
我的代码:
//pattern to find if there is any special character in string
Pattern regex = Pattern.compile("[$&+,:;[email protected]#|'<>.-^*()%!]");
//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
**所以你认为现存的唯一特殊字符是键盘上的字符**!** :-) – xanatos
没错。最好定义所有“非特殊”字符并作出否定。 – NeplatnyUdaj