Regex for checking if a string is strictly alphanumeric

O__O picture O__O · Jun 28, 2012 · Viewed 180.9k times · Source

How can I check if a string contains only numbers and alphabets ie. is alphanumeric?

Answer

Raghav picture Raghav · Jun 28, 2012

Considering you want to check for ASCII Alphanumeric characters, Try this: "^[a-zA-Z0-9]*$". Use this RegEx in String.matches(Regex), it will return true if the string is alphanumeric, else it will return false.

public boolean isAlphaNumeric(String s){
    String pattern= "^[a-zA-Z0-9]*$";
    return s.matches(pattern);
}

If it will help, read this for more details about regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html