这有点晚了,所以这只是一个观察。
这是@Tim Pietzcker 方法的起飞。
虽然 'words' 可以是任何东西,但如果您希望至少
3 个单词嵌入 [a-zA-Z0-9]{2,40} 个字符,您可以这样做。
^ # String start
(?=.*[@#$%^&+=]) # Assert 1 special char
(?=.*\d) # Assert 1 digit
(?: # Special 'Word Group' -- Need 2 words
.* # Any char, 0 or more times
[a-zA-Z0-9]{2,40} # Alpha/num char, 2 to 40 times
.* # Any char, 0 or more times
\s # a whitespace, only 1 required
){2} # 'Word Group' end, do 2 times
.* # Any char, 0 or more times
[a-zA-Z0-9]{2,40} # Alpha/num char, 2 to 40 times -- Need 1 word
这应该匹配至少 3 个特殊的 [a-zA-Z0-9]{2,40} 单词,由至少 1 个空格分隔,
包括一个数字和特殊字符。
更新
是的,您可以用我知道的 2 种方法将它组合成一个组,完成 {3} 次。
使用捕获缓冲区作为标志
^(?=.*[@#$%^&+=])(?=.*\d)(?:(?:(?!\1)|\s).*[a-zA-Z0-9]{2,40}().*){3}
^ ^
---------------------------------------
^ # String start
(?=.*[@#$%^&+=]) # Assert 1 special char
(?=.*\d) # Assert 1 digit
(?: # Special 'Word Group'
(?: #.. grping start ....
(?!\1) # Either capt group 1 is UN-DEFINED
| \s # OR, require a whitespace
) #.. grping end ....
.* # Any char, 0 or more times
[a-zA-Z0-9]{2,40} # Alpha/num char, 2 to 40 times
() # DEFINE Capture group 1
.* # Any char, 0 or more times
){3} # 'Word Group' end, do 3 times
或者,通过使用条件
^(?=.*[@#$%^&+=])(?=.*\d)(?:(?(1)\s).*([a-zA-Z0-9]{2,40}).*){3}
^ ^
---------------------------------------
^ # String start
(?=.*[@#$%^&+=]) # Assert 1 special char
(?=.*\d) # Assert 1 digit
(?: # Special 'Word Group'
(?(1)\s) # Conditional, require a whitespace if capture group 1 captured anything
.* # Any char, 0 or more times
([a-zA-Z0-9]{2,40}) # Capture group 1, Alpha/num char, 2 to 40 times
.* # Any char, 0 or more times
){3} # 'Word Group' end, do 3 times