确保给定字符串至少包含以下每个类别中的一个字符的正则表达式是什么。
我知道各个集合的模式,即[a-z],和(我把它们弄对了[A-Z],不是吗?)。\d``_|[^\w]
[a-z]
[A-Z]
\d``_|[^\w]
但是我如何组合它们以确保字符串以任何顺序包含所有这些?
如果您需要一个正则表达式,请尝试:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)
一个简短的解释:
(?=.*[a-z]) // use positive look ahead to see if at least one lower case letter exists (?=.*[A-Z]) // use positive look ahead to see if at least one upper case letter exists (?=.*\d) // use positive look ahead to see if at least one digit exists (?=.*\W) // use positive look ahead to see if at least one non-word character exists
我同意 SilentGhost,\W可能有点宽泛。我会用这样的字符集替换它:([-+_!@#$%^&*.,?]当然可以随意添加更多!)
\W
[-+_!@#$%^&*.,?]