public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Pattern.matches("[^A-Za-z0-9]","@#%abc"));
}
This is very very simple code about regex in Java.
As far as I know, [^A-Za-z0-9] should return true when it matches with any special characters because [^ means the negation and A-Za-z0-9 means all characters including numbers. I don’t know why above code keeps returning false, instead of true.
Add a +:
Pattern.matches()implies a full match, i.e. the entire pattern matches the text from beginning to end. In your case you are doing a find(), i.e. there are multiple matches of the Pattern in the text, but not a single full match, as your pattern matches only one character.