有人可以告诉我正则表达式的语法是什么,只允许以下字符:
a-z
A-Z
0-9
dash
underscore
此外,字符串不能包含超过 5 个数字。
提前感谢您的帮助!
您需要
的正则表达式是
^[a-zA-Z0-9_-]{0,5}$
它匹配最多五个字符的任意字符组合。
几种可能性:
~'A(?:[a-z_-]*[0-9]){0,5}[a-z_-]*'z(?<=.)~i
或
~'A(?!(?:.*[0-9]){6})['w-]+'z~
这两种模式假定不允许使用空字符串。
第一种模式:
~ # pattern delimiter
'A # anchor for the start of the string
(?:[a-z_-]*[0-9]){0,5} # repeat this group between 0 or 5 times (so 5 digits max)
[a-z_-]* # zero or more allowed characters
'z # end of the string
(?<=.) # lookbehind that checks there is at least one character
~
i # make the pattern case insensitive
第二种模式:
~
'A
(?! # negative lookahead that checks there is not
(?:.*[0-9]){6} # 6 digits in the string
)
['w-]+
'z
~