简体   繁体   English

至少一位数字的字母数字正则表达式

[英]Regular expression for alphanumeric with atleast one digit

I am trying to match an alphanumeric string with at least one digit. 我正在尝试将字母数字字符串与至少一位数字匹配。 The second condition is that it's minimum length should be 3. 第二个条件是它的最小长度应为3。

For example, the following strings should match 例如,以下字符串应匹配

111
12345
ABCD1
123A
11AA11

And the following should not match 并且以下不匹配

ABCD
AB
12
1A

I have got myself to a point where I can get the first condition right. 我已经达到了可以使第一个条件正确的地步。 That is, having minimum one digit: 也就是说,至少有一位数字:

([a-zA-z0-9]*[0-9]+[a-zA-z0-9]*)

But I don't have any idea to specify a minimum length. 但是我不知道指定最小长度。 If I try using {3}, it will require minimum of 3 numbers. 如果我尝试使用{3},则至少需要3个数字。

尝试使用正向前瞻来确定至少有一位数字,并使用{3,}表示它应至少匹配3个字符:

/^(?=.*\d)[a-z\d]{3,}$/i

您可以使用lookahead来确保表达式中包含一个数字,然后匹配最少三个字符:

/^(?=.*?\d)[a-zA-Z0-9]{3,}$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM