What is the correct syntax for matching all characters except specific ones.
For example I'd like to match everything but letters [A-Z] [a-z]
and numbers [0-9]
.
I have
string.matches("[^[A-Z][a-z][0-9]]")
Is this incorrect?
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).
You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.
Yes, you don't need nested []
like that. Use this instead:
"[^A-Za-z0-9]"
It's all one character class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With