Java regex matches all characters except

What is the correct syntax for matching all but certain characters.

For example, I would like to combine everything except the letters [AZ] [az] and the numbers [0-9] .

I have

 string.matches("[^[AZ][az][0-9]]") 

This is not true?

+8
java regex
source share
5 answers

Yes, you do not need a nested [] like this. Use this instead:

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

These are all one character classes.

+18
source share

If you want to match anything other than letters, you should study the Unicode properties .

\p{L} - any letter from any language

Using the uppercase β€œP” instead is a negation, so \p{L} matches everything that is not a letter.

\d or \p{Nd} matches digits

So your expression in a modern Unicode style will look like this:

Or using a negative character class

 [^\p{L}\p{Nd}] 

or negative properties

 [\P{L}\P{Nd}] 

Next, matches() matches an expression against a complete string, so your expression is true with only one char character per line. Therefore, you need to add a quantifier:

 string.matches("[^\p{L}\p{Nd}]+") 

returns true when the full string has only non-alphabetic characters and at least one of them.

+6
source share
 string.matches("[^A-Za-z0-9]") 
+1
source share

Almost correct. Do you want to:

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

Here is a good tutorial

+1
source share

Suppose you want to make sure that none of the lines have the _ character in them, then you will just use something like this.

  Pattern pattern = Pattern.compile("_"); Matcher matcher = Pattern.matcher(stringName); if(!matcher.find()){ System.out.println("Valid String"); }else{ System.out.println("Invalid String"); } 
0
source share

All Articles