[英]REGEX a-z 0-9 but not only numbers
Trying to match only strings 10-30 characters long with only az and 0-9 (but not only numbers) in the string. 尝试仅匹配10-30个字符长的字符串,仅匹配字符串中的az和0-9(但不仅仅是数字)。 Seems to work Except when the string starts with a number then it fails.
似乎工作除了字符串以数字开头然后失败。 Not sure about the \\D which should fix the not only numbers
不确定\\ D应该修复不仅数字
static final Pattern UNIQUE_ID_PATTERN = Pattern.compile("^\\D[A-Za-z0-9_-]{10,30}$");
UNIQUE_ID_PATTERN.matcher("1eeeeeeeee333e").matches(); // Does not work
UNIQUE_ID_PATTERN.matcher("eeeeeeeee333e").matches(); // Works
The \\D
shorthand class means any non-digit symbol
. \\D
简写类表示any non-digit symbol
。 You should remove it from the pattern (so that it becomes "^[A-Za-z0-9_-]{10,30}$"
) for the matches
to return true, as 1
is a digit in 1eeeeeeeee333e
. 您应该从模式中删除它(以便它变为
"^[A-Za-z0-9_-]{10,30}$"
)以使matches
返回true,因为1
是1eeeeeeeee333e
的数字。
If you want to place a restriction (the string cannot consist of only digits) use an anchored look-ahead: 如果要设置限制(字符串不能只包含数字),请使用锚定前瞻:
^(?![0-9]+$)[A-Za-z0-9_-]{10,30}$
Or, a shortened version with i
modifier making the pattern case-insensitive: 或者,带有
i
修饰符的缩短版本使模式不区分大小写:
(?i)^(?![0-9]+$)[A-Z0-9_-]{10,30}$
Another way: ^(?=(?:.*[a-zA-Z])+)([a-zA-Z\\d]{10,30})$
update 另一种方式:
^(?=(?:.*[a-zA-Z])+)([a-zA-Z\\d]{10,30})$
update
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.