这可能很困难。
标准grep
的功能有限。它只支持 POSIX 扩展的正则表达式,它不能识别你的正则表达式所依赖的前瞻断言。
如果你grep
的机器上有 GNU,你可以给它传递-P
or--perl-regexp
参数,允许它使用 Perl 兼容的正则表达式。那么你的正则表达式应该可以工作。
正如我在评论中提到的,正则表达式不适合密码验证。它允许密码z000
,甚至是空字符串:
( # Either match and capture...
^ # Start of the string
( # Match (and capture, uselessly in this case)
[zZ] # case-insensitive z
\d{3} # three digits
)* # zero(!) or more times
$ # until the end of the string
) # End of first group.
| # OR
( # match and capture...
(?=.{9,}) # a string that's at least 9 characters long,
(?=.*?[^\w\s]) # contains at least one non-alnum, non-space character,
(?=.*?[0-9]) # at least one ASCII digit
(?=.*?[A-Z]) # at least one ASCII uppercase letter
.*?[a-z].* # and at least one ASCII lowercase letter
) # no anchor to start/end of string...
更好的使用
^(?=.{9})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*$