Regex for checking if a string is strictly alphanumeric

前端 未结 10 2043
予麋鹿
予麋鹿 2020-12-01 12:02

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

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 12:08

    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

提交回复
热议问题