Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a regex for accepting only alphanumeric characters? [duplicate]

Tags:

java

regex

Possible Duplicate:
Regular Expression for alphanumeric and underscores

How to create a regex for accepting only alphanumeric characters?

Thanks.

like image 792
MarkJ Avatar asked May 13 '11 06:05

MarkJ


People also ask

How do I allow only special characters in regex?

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


2 Answers

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

like image 130
niksvp Avatar answered Oct 05 '22 21:10

niksvp


[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]*$"; 
like image 43
Frank Schmitt Avatar answered Oct 05 '22 19:10

Frank Schmitt