开发者

Java simple regular expression

开发者 https://www.devze.com 2023-02-11 16:45 出处:网络
I want to specify a regex that finds if there are any non letter non number chars in a Str开发者_开发问答ing.

I want to specify a regex that finds if there are any non letter non number chars in a Str开发者_开发问答ing.

Basically I want it to accept [a-z][A-Z][0-9] in any order any combination..i.e "2a4A44awA" should be valid.

How can I do this?


Instead of:

[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


String s = ".... ;

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

returns true if illegal character is present.

Edit: But the first answer from jzd is better:

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

Returns true if illegal character not present, ie the string is good.

0

精彩评论

暂无评论...
验证码 换一张
取 消