简体   繁体   English

正则表达式以限制字符和数字

[英]Regular expression to limit character & number

my regular expression requirement is number and character combined exp. 我的正则表达式要求是数字和字符组合的exp。 and this limited 5~15 characters. 并限制在5到15个字符之间。

for example, 例如,

abcd1, abc1d, a21ab, 1abcd, abcvda123 ...
ABCd1, Abc1d, 

not allowed exampled, 不允许举例,

abcd!1, !adf2a, abcd!a, abcd!2 ...
abc1, ab1c, 1abc, !abc ...

my regex exp is 我的正则表达式exp是

^(?=.+[a-zA-Z])(?=.+[0-9]).{5,15}$

but is too bad. 但是太糟糕了。

Change .{5,15} to [a-zA-Z0-9]{5,15} . .{5,15}更改为[a-zA-Z0-9]{5,15}

Also, in the lookaheads, change .+ to .* . 此外,在前瞻中,将.+更改为.* Otherwise, it won't match if the only character of the type after that is the first character in the string. 否则,如果此类型之后的唯一字符是字符串中的第一个字符,则它将不匹配。 So the resulting regexp is: 因此,生成的正则表达式为:

^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{5,15}$

DEMO 演示

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

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