Regex for checking if a string is strictly alphanumeric

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

    See the documentation of Pattern.

    Assuming US-ASCII alphabet (a-z, A-Z), you could use \p{Alnum}.

    A regex to check that a line contains only such characters is "^[\\p{Alnum}]*$".

    That also matches empty string. To exclude empty string: "^[\\p{Alnum}]+$".

提交回复
热议问题