我想编写一个正则表达式来检查字符串是否在任何地方都有 3 个大写字母、3 个数字和 3 个特殊字符(&%#@!$)。这些字母不必一个接一个。字符串的长度也应该至少为 10。也不应该有任何空格。
所以那个字符串
aF2$Rec45yT&! - will match
aF2$Re c45yT&! - will not match
F2$R45T&! - will not match
Run Code Online (Sandbox Code Playgroud)
此正则表达式符合要求:
^(?=(.*?[A-Z]){3})(?=(.*?[0-9]){3})(?=(.*?[&%#@!$]){3})\S{10,}$
Run Code Online (Sandbox Code Playgroud)
组件是:
(?=(.*?[A-Z]){3}) - lookahead to make sure there are at least 3 upper case letters
(?=(.*?[0-9]){3}) - lookahead to make sure there are at least 3 digits
(?=(.*?[&%#@!$]){3}) - lookahead to make sure there are at least 3 of the listed special characters
\S{10,} - to make sure there is no space in input and it is at least 10 in lenght
Run Code Online (Sandbox Code Playgroud)