ASP.net中强密码的正则表达式我需要检查包含以下4种中的3种的密码:小写字母大写字母数字字符特殊字符(如%,$,#,...)密码长度必须在6到20个字符之间。我目前有这个:publicvoidChangePassword(stringpassword){Regexregex1=newRegex("^(?=.*[0-9])(?=.*[az])(?=.*[A-Z]){6,20}$");正则表达式regex2=newRegex("^(?=.*[0-9])(?=.*[az])(?=.*?[#?!@$%^&*-]){6,20}$");正则表达式regex3=newRegex("^(?=.*[0-9])(?=.*[AZ])(?=.*?[#?!@$%^&*-]){6,20}$");正则表达式regex4=newRegex("^(?=.*[az])(?=.*[AZ])(?=.*?[#?!@$%^&*-]){6,20}$");正则表达式regex5=newRegex("^(?=.*[0-9])(?=.*[az])(?=.*[AZ])(?=.*?[#?!@$%^&*-]){6,20}$");匹配match1=regex1.Match(密码);匹配match2=regex2.Match(密码);匹配match3=regex3.Match(密码);匹配match4=regex4.Match(密码);匹配match5=regex5.Match(密码);如果(match1.Success||match2.Success||match3.Success||match4.Success||match5.Success){Password=password;}else{抛出新的PasswordNotGoodException();然而,这根本不匹配。这是一个学校项目,所以我真的需要一些帮助。您可以使用以下内容代替REGEX:stringpassword="aA1%";HashSetspecialCharacters=newHashSet(){'%','$','#'};if(password.Any(char.IsLower)&&//小写密码。Any(char.IsUpper)&&password.Any(char.IsDigit)&&password.Any(specialCharacters.Contains)){//validpassword}更简单和清洁剂。编辑:如果这4个条件中至少有3个为真,您可以这样做:intconditionsCount=0;如果(password.Any(char.IsLower))conditionsCount++;如果(password.Any(char.IsUpper))conditionsCount++;if(password.Any(char.IsDigit))conditionsCount++;如果(password.Any(specialCharacters.Contains))conditionsCount++;if(conditionsCount>=3){//validpassword}这里最后一次重复是错误的:Regexregex1=newRegex("^(?=.*[0-9])(?=.*[az])(?=.*[AZ]){6,20}$");更改为:Regexregex1=newRegex("^(?=.*[0-9])(?=.*[az])(?=.*[AZ]).{6,20}$");//注意这里的点___^所有的正则表达式都是一样的。以上就是《C#学习教程:ASP.net中正则表达式的强密码》的全部内容。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
