Java regex match all characters except
Yes, you don't need nested []
like that. Use this instead:
"[^A-Za-z0-9]"
It's all one character class.
string.matches("[^A-Za-z0-9]")
If you want to match anything but letters, you should have a look into Unicode properties.
\p{L}
is any kind of letter from any language
Using an uppercase "P" instead it is the negation, so \P{L}
would match anything that is not a letter.
\d
or \p{Nd}
is matching digits
So your expression in modern Unicode style would look like this
Either using a negated character class
[^\p{L}\p{Nd}]
or negated properties
[\P{L}\P{Nd}]
The next thing is, matches()
matches the expression against the complete string, so your expression is only true with exactly one char in the string. So you would need to add a quantifier:
string.matches("[^\p{L}\p{Nd}]+")
returns true, when the complete string has only non alphanumerics and at least one of them.
Almost right. What you want is:
string.matches("[^A-Za-z0-9]")
Here's a good tutorial