probably I misunderstood, then it is not possible to have a simple regular expression to validate such conditions, unless you want to use combinations like类1字符.*类2字符.*类3字符|类1字符.*类3字符.*类2字符|类3字符.*类2字符.*类1字符|类1字符.*类2字符.*类4字符|....which is not a good solution
string[] slist = {"AAAAA@AAA","Aabc6745","ABCDEFAB","ABCDEFZB","456","AAAbbb888z"}; Regex re = new Regex(@"^((?<lower>[a-z])|(?<upper>[A-Z])|(?<number>[0-9])|(?<special>[%@])){8,}$"); foreach(string s in slist) { Match m = re.Match(s); if (m.Success) { int n =0; if (m.Groups["lower"].Captures.Count > 0) n++; if (m.Groups["upper"].Captures.Count > 0) n++; if (m.Groups["number"].Captures.Count > 0) n++; if (m.Groups["special"].Captures.Count > 0) n++; if (n>=3) Console.WriteLine("{0} matches", s); else Console.WriteLine("{0} doesn't match", s); } else Console.WriteLine("{0} doesn't match", s); }
瓦靠,saucer就是牛啊。不过我对于正则表达式比较弱,所有我用了4个正则表达式。思路和saucer一样,但是saucer一个表达式就搞定了,又学习了。 下面我的算法: ================================================================================ public override bool PasswordValidate(string pwd,ref string msg) { //单独判断长度只是为了能明确了知道是由于长度不合法 if ( pwd.Length < 8 ) { msg = "Password must be more than 8 characters."; return false; } Regex regex1 = new Regex(@"[A-Z]",RegexOptions.Compiled); Regex regex2 = new Regex(@"[a-z]",RegexOptions.Compiled); Regex regex3 = new Regex(@"\d",RegexOptions.Compiled); Regex regex4 = new Regex(@"\W",RegexOptions.Compiled); int seed = 0; if ( regex1.IsMatch(pwd) ) seed ++; if ( regex2.IsMatch(pwd) ) seed ++; if ( regex3.IsMatch(pwd) ) seed ++; if ( regex4.IsMatch(pwd) ) seed ++; if ( seed > 2 ) { return true; } else { msg = "Password must be consisted of Three of the Following Four Style:A-Z,a-z,0-9,Special Characters."; return false; } }
仅“大写字母,小写字母,数字”的话:
^[A-Za-z0-9]{8,}$
那个特殊字符暂时不考虑,那就是:Aabc6745是合法的需要组合的。
Regex.IsMatch( str , "[A-Z]" ) &&
Regex.IsMatch( str , @"\d" ) &&
Regex.IsMatch( str , @"[,<>?/;'等等]" )
{
OK;
}
>>>Aabc6745是合法的是不能重复么?
string[] slist = {"AAAAAAAA","Aabc6745","ABCDEFAB","ABCDEFZB","456"}; foreach(string s in slist)
{
Console.WriteLine("{0} matches? {1}", s, Regex.IsMatch(s, @"^(?!.*(.).*\1)[\u0021-\u00FE]{8}$"));
}
一个字符串,长度必须在8位以上。
然后必须由以下4类字符的3类组成:大写字母,小写字母,数字,特殊字符。按照上面的表达,我翻译一下:
1、长度要大于8
2、字符分为4类:a. 大写字母,b. 小写字母,c. 数字,d. 特殊字符
3、符合条件的字符串必须有上述4类字符串的3类组成(不知道是不是包含3类以上),如AAAAAAAAA,bbbbbbbbbbb, 8888888888等都不合法,但是AAAbbb888就是合法的。
{
Match m = re.Match(s);
if (m.Success)
{
int n =0;
if (m.Groups["lower"].Captures.Count > 0)
n++; if (m.Groups["upper"].Captures.Count > 0)
n++; if (m.Groups["number"].Captures.Count > 0)
n++; if (m.Groups["special"].Captures.Count > 0)
n++;
if (n>=3)
Console.WriteLine("{0} matches", s);
else
Console.WriteLine("{0} doesn't match", s);
}
else
Console.WriteLine("{0} doesn't match", s);
}
下面我的算法:
================================================================================
public override bool PasswordValidate(string pwd,ref string msg)
{
//单独判断长度只是为了能明确了知道是由于长度不合法
if ( pwd.Length < 8 )
{
msg = "Password must be more than 8 characters.";
return false;
}
Regex regex1 = new Regex(@"[A-Z]",RegexOptions.Compiled);
Regex regex2 = new Regex(@"[a-z]",RegexOptions.Compiled);
Regex regex3 = new Regex(@"\d",RegexOptions.Compiled);
Regex regex4 = new Regex(@"\W",RegexOptions.Compiled);
int seed = 0;
if ( regex1.IsMatch(pwd) )
seed ++;
if ( regex2.IsMatch(pwd) )
seed ++;
if ( regex3.IsMatch(pwd) )
seed ++;
if ( regex4.IsMatch(pwd) )
seed ++;
if ( seed > 2 )
{
return true;
}
else
{
msg = "Password must be consisted of Three of the Following Four Style:A-Z,a-z,0-9,Special Characters.";
return false;
}
}