Simple Java Regular Expression

I want to specify a regular expression that finds if there are any letters in the string with the letter non number.

Basically, I want him to take [a-z][A-Z][0-9]in any order in any combination. ie "2a4A44awA"must be valid.

How can i do this?

+5
source share
2 answers

Instead:

[a-z][A-Z][0-9] 

Match with:

[a-zA-Z0-9]+

From the API:

Greedy quantifiers
X?  X, once or not at all
X*  X, zero or more times
X+  X, one or more times
X{n}    X, exactly n times
X{n,}   X, at least n times
X{n,m}  X, at least n but not more than m times
+7
source
String s = ".... ;

System.out.println(s.matches(".*[^a-zA-z0-9].*"));

returns true if an illegal character is present.

Edit: But the first answer from jzd is better:

s.matches("[a-zA-Z0-9]+");

Returns true if an invalid character is missing, i.e. the line is good.

+3
source

All Articles