Regex for checking if a string is strictly alphanumeric

前端 未结 10 2019
予麋鹿
予麋鹿 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:29

    Use character classes:

    ^[[:alnum:]]*$
    
    0 讨论(0)
  • 2020-12-01 12:29
    Pattern pattern = Pattern.compile("^[a-zA-Z0-9]*$");
    Matcher matcher = pattern.matcher("Teststring123");
    if(matcher.matches()) {
         // yay! alphanumeric!
    }
    
    0 讨论(0)
  • 2020-12-01 12:33

    100% alphanumeric RegEx (it contains only alphanumeric, not even integers & characters, only alphanumeric)

    For example:

    special char (not allowed)
    123 (not allowed)
    asdf (not allowed)
    1235asdf (allowed)


    String name="^[^<a-zA-Z>]\\d*[a-zA-Z][a-zA-Z\\d]*$";
    
    0 讨论(0)
  • 2020-12-01 12:35

    In order to be unicode compatible:

    ^[\pL\pN]+$
    

    where

    \pL stands for any letter
    \pN stands for any number
    
    0 讨论(0)
提交回复
热议问题