要求密码的正则表达式,需要一个数字或一个非字母数字字符

Chr*_*col 7 regex

我正在寻找一个相当具体的正则表达式,我几乎拥有它但不完全.

我希望有一个正则表达式,这将需要至少5个charactors,在这些字符中的至少一个是任意一个数值或者一个非字母数字字符.

这是我到目前为止:

^(?=.*[\d]|[!@#$%\^*()_\-+=\[{\]};:|\./])(?=.*[a-z]).{5,20}$
Run Code Online (Sandbox Code Playgroud)

所以问题是"或"部分.它将允许非字母数字值,但仍需要至少一个数值.你可以看到我有或运算符"|" 在我需要的数字和非字母数字之间,但似乎没有用.

任何建议都会很棒.

Bar*_*ers 19

尝试:

^(?=.*(\d|\W)).{5,20}$
Run Code Online (Sandbox Code Playgroud)

一个简短的解释:

^                         # match the beginning of the input
(?=                       # start positive look ahead
  .*                      #   match any character except line breaks and repeat it zero or more times
  (                       #   start capture group 1
    \d                    #     match a digit: [0-9]
    |                     #     OR
    \W                    #     match a non-word character: [^\w]
  )                       #   end capture group 1
)                         # end positive look ahead
.{5,20}                   # match any character except line breaks and repeat it between 5 and 20 times
$                         # match the end of the input
Run Code Online (Sandbox Code Playgroud)

  • ...如果你的意思是*"一个数字**和**非字母数字字符"*,那么这将成功:`^(?=.*\d)(?=.*\W). {5,20} $` (2认同)