This Works。
(注 - 建議您排除空白以及)
# ^(?=.{8}$)(?!\d)(?=\PP*$)(?=(.)(?!\1\1))(?=.*[A-Z])(?=.*[a-z])(?=(?:\D*\d){2,4}\D*$)(?=.*([a-zA-Z]).*(?!\2)([a-zA-Z]).*(?!\2|\3)[a-zA-Z]).*$
^ # BOS
(?= .{8} $) # 8 characters total
(?! \d) # Not start with digit
(?= \PP* $) # No Punct (special chars?)
(?= # Not start 3 identical chars
(.) # (1)
(?! \1 \1)
)
(?= .* [A-Z]) # At least 1 upper
(?= .* [a-z]) # At least 1 lower
(?= # At least 2 digits, no more than 4
(?: \D* \d){2,4}
\D* $
)
(?= # At least 3 different alpha chars
.*
([a-zA-Z]) # (2)
.*
(?! \2)
([a-zA-Z]) # (3)
.*
(?! \2 | \3)
[a-zA-Z]
)
.*
$ # EOS
推薦 - RegexFormat4讀/維護的代碼。
Perl的測試用例
if('~5bC$=6c' =~ /^(?=.{8}$)(?!\d)(?=\PP*$)(?=(.)(?!\1\1))(?=.*[A-Z])(?=.*[a-z])(?=(?:\D*\d){2,4}\D*$)(?=.*([a-zA-Z]).*(?!\2)([a-zA-Z]).*(?!\2|\3)[a-zA-Z]).*$/)
{
print "Matched $&\n";
}
輸出>>
Matched ~5bC$=6c
來源
2014-03-07 16:11:22
sln
〔羅馬密碼的複雜性,蝙蝠俠](http://xkcd.com/936/) – admdrew
如果你這樣做作爲一個「正則表達式練習」,否則,正確的做法是使用具有不同測試的'validate()'方法檢查所有需求。將來閱讀和維護會更容易(如果您決定將其更改爲需求)。 – alfasin
另外,給出這個龐大的限制列表的可能的密碼集......在一定程度上是有限的。 – Tetrinity