Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex match all characters except

Tags:

java

regex

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?

like image 786
Tommy Avatar asked Apr 25 '13 06:04

Tommy


People also ask

How do you match a character except in regex?

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).

How do I allow only special characters in regex?

You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.


3 Answers

Yes, you don't need nested [] like that. Use this instead:

"[^A-Za-z0-9]"

It's all one character class.

like image 177
Paul Avatar answered Oct 21 '22 14:10

Paul


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.

like image 30
stema Avatar answered Oct 21 '22 16:10

stema


Almost right. What you want is:

string.matches("[^A-Za-z0-9]")

Here's a good tutorial

like image 27
Miquel Avatar answered Oct 21 '22 16:10

Miquel