我需要一个用于密码的字符串的正则表达式,该字符串至少包含以下四种类型中的三个字符:
并且应该至少包含8个字符。
发布于 2011-06-21 11:05:33
正确回答我的问题:
正则表达式:
^((?=.\d)(?=.a-z)(?=.A-Z)|(?=.a-z)(?=.A-Z)(?=.^\w\d\s)|(?=.\d)(?=.A-Z)(?=.^\w\d\s)|(?=.\d)(?=.a-z)(?=.^\w\d\s)).{8,30}$
谢谢。
发布于 2011-05-26 11:47:25
regex的扩展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发布于 2011-05-26 18:23:16
首先,下面是帕克斯辉绿罗代码的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 Regex平衡组的乐趣上的模式
^
(?=.*[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}https://stackoverflow.com/questions/6134195
复制相似问题