How to create a regex for accepting only alphanumeric characters? [duplicate]
Possible Duplicate:
Regular Expression for alphanumeric and underscores
How to create a regex for accepting only alphanumeric characters?
T开发者_StackOverflow社区hanks.
Try below Alphanumeric regex
"^[a-zA-Z0-9]*$"
^ - Start of string
[a-zA-Z0-9]* - multiple characters to include
$ - End of string
See more: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
[a-zA-Z0-9] will only match ASCII characters, it won't match
String target = new String("A" + "\u00ea" + "\u00f1" +
"\u00fc" + "C");
If you also want to match unicode characters:
String pat = "^[\\p{L}0-9]*$";
Only ASCII or are other characters allowed too?
^\w*$
restricts (in Java) to ASCII letters/digits und underscore,
^[\pL\pN\p{Pc}]*$
also allows international characters/digits and "connecting punctuation".
try with \w
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
Use this ^[a-zA-Z0-9_]*$
See here for more info.
see http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
for example [A-Za-z0-9]
精彩评论