以下约束的正则表达式是什么?

| 我想要一个密码字符串的正则表达式,该字符串至少包含以下四种字符中的三种: 英文小写字母(a到z)。 英语大写字符(A到Z)。 以10为基数(0到9)。 非字母数字字符(例如,!,$,#,%)。 并且应至少包含8个字符。     
已邀请:
考虑到可能会产生这些要求,我怀疑您通过进行多次检查实际上会更好一些,例如(伪代码):
def check_password (s):
    if len(s) < 8:
        return false
    rules_followed = 0
    if match (s, \"[a-z]\") rules_followed++;
    if match (s, \"[A-Z]\") rules_followed++;
    if match (s, \"[0-9]\") rules_followed++;
    if match (s, \"[!$#%]\") rules_followed++;  # et cetera
    if rules_followed < 3:
        return false
    return true
当其他人必须维护您的代码(或者即使您必须从现在开始六个月维护代码)时,这也可能更具可读性。 现在,我意识到这可能并不总是可行的(例如,您可能只限于一个只允许使用一个正则表达式进行验证的框架)。 但是,如果可能的话,我强烈建议您考虑一下。您应始终尝试进行优化的第一件事是可读性。 a您可能会得到大量超前的正则表达式中的一个,或者包含以
|
分隔的十六种排序可能性的单个正则表达式。 这些都不像简单的代码段那样高效或可读。     
正则表达式的扩展PCRE版本:
/^(?:
  (?=.*[a-z])         # look ahead: at least one from a-z
  (?=.*[A-Z])         # look ahead: at least one from A-Z
  (?=.*[0-9])         # look ahead: at least one from 0-9
  |                   # or...
  (?=.*[a-z])         # look ahead: at least one from a-z
  (?=.*[A-Z])         # look ahead: at least one from A-Z
  (?=.*[^a-zA-Z0-9])  # look ahead: at least one from special chars
  |                   # or...
  (?=.*[a-z])         # look ahead: at least one from a-z
  (?=.*[0-9])         # look ahead: at least one from 0-9
  (?=.*[^a-zA-Z0-9])  # look ahead: at least one from special chars
  |                   # or...
  (?=.*[A-Z])         # look ahead: at least one from A-Z
  (?=.*[0-9])         # look ahead: at least one from 0-9
  (?=.*[^a-zA-Z0-9])  # look ahead: at least one from special chars
  )
  \\S{8,}              # at least 8 non-spaces
$/x
    
首先,这是paxdiablo的代码的C#转换:
public bool Validate(string input)
{
    if (input == null || input.Length < 8)
        return false;
    int counter = 0;
    if (input.Any(Char.IsLower)) counter++;
    if (input.Any(Char.IsUpper)) counter++;
    if (input.Any(Char.IsDigit)) counter++;
    if (input.Any(c => Char.IsPunctuation(c) || Char.IsSymbol(c))) counter++;
    return counter >= 3;
}
如果您坚持使用正则表达式,则可以使用与.NET正则表达式平衡组的乐趣类似的模式:
^
(?=.*[a-z](?<Counter>))?
(?=.*[A-Z](?<Counter>))?
(?=.*[0-9](?<Counter>))?
(?=.*[^a-zA-Z0-9](?<Counter>))?
(?<-Counter>){3}   # check we\'ve had at least 3 groups
.{8}
您还可以允许Unicode类:
^
(?=.*\\p{Ll}(?<Counter>))?
(?=.*\\p{Lu}(?<Counter>))?
(?=.*\\p{Nd}(?<Counter>))?
(?=.*[\\p{S}\\p{P}](?<Counter>))?
(?<-Counter>){3}
.{8}
    
组合功能还不错-只有四种方法可以从四种可能性中选择三种。您可以在正则表达式的开头先行测试,然后检查实际匹配的八个字符:
^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)|(?=.*[A-Z])(?=.*[a-z])(?=.*[_\\W])|(?=.*[A-Z])(?=.*\\d)(?=.*[_\\W])|(?=.*[a-z])(?=.*\\d)(?=.*[_\\W])).{8}
(锚定是为了在失败时提高效率,没有它就可以工作)。 密码可能无关紧要,但是对于相同类型的字符,上述版本可能会多次浏览整个字符串,尤其是在失败时。您可能会以可读性为代价将其排除在外:
^(?:(?=.*[A-Z])(?:(?=.*[a-z])(?:(?=.*\\d)|(?=.*[_\\W]))|(?=.*\\d)(?=.*[_\\W]))|(?=.*[a-z])(?=.*\\d)(?=.*[_\\W])).{8}
    
我的问题的正确答案: 正则表达式 : ^((?=。[\\ d])(?=。[az])(?=。[AZ])|(?=。[az])(?=。[AZ])(?=。[ ^ \\ w \\ d \\ s])|(?=。[\\ d])(?=。[AZ])(?=。[^ \\ w \\ d \\ s])|( ?=。[\\ d])(?=。[az])(?=。[^ \\ w \\ d \\ s])))。{8,30} $ 谢谢。     

要回复问题请先登录注册