C# :无效 密码[7]=“123456789”;//编号:无效 //密码验证模式 var hasLetterAndNumber=新正则表达式(@“(?=.[A-Z])|(?=.[A-Z]))(?=.[0-9]){8,32}”); var hasLetterAndCharacter=new Regex(@“((?=.*[A-Z])|(?=.*[A-Z]))(?=.[@$%^&+=])。{8,32}); var hasNumberAndCharacter=new Regex(@“(?=.[0-9])(?=.[@$%^&+=])。{8,32}); //验证密码 for(int i=0;i
您不需要正则表达式。尽管我发布了答案,但您确实应该看看漂亮的细分。我接受了你的最后一句话,让它更优雅,如果你同意的话<代码>^(?=[A-Za-z]*)(?=[0-9]*)(?=[@$%^&+=]*)。{8,32}$@KarolFlis是的,确实那些C# :无效 密码[7]=“123456789”;//编号:无效 //密码验证模式 var hasLetterAndNumber=新正则表达式(@“(?=.[A-Z])|(?=.[A-Z]))(?=.[0-9]){8,32}”); var hasLetterAndCharacter=new Regex(@“((?=.*[A-Z])|(?=.*[A-Z]))(?=.[@$%^&+=])。{8,32}); var hasNumberAndCharacter=new Regex(@“(?=.[0-9])(?=.[@$%^&+=])。{8,32}); //验证密码 for(int i=0;i,c#,regex,C#,Regex,您不需要正则表达式。尽管我发布了答案,但您确实应该看看漂亮的细分。我接受了你的最后一句话,让它更优雅,如果你同意的话^(?=[A-Za-z]*)(?=[0-9]*)(?=[@$%^&+=]*)。{8,32}$@KarolFlis是的,确实那些*看起来并不相关。将您的评论包括在答案中。@KarolFlis我不得不将答案更改回去,因为它没有强制执行所有规则。您是对的,谢谢您让我在regexp上刷新我的记忆:) ^ Matches the start of t
*
看起来并不相关。将您的评论包括在答案中。@KarolFlis我不得不将答案更改回去,因为它没有强制执行所有规则。您是对的,谢谢您让我在regexp上刷新我的记忆:)
^ Matches the start of the string
(?=.{8,}) Matches when there are at least 8 characters
(?=.*[a-z]) Matches when there is a lowercase letter anywhere
(?=.*[A-Z]) Matches when there is an uppercase letter anywhere
(?=.*[@#$%^&+=]) Matches when there is one of the indicated characters anywhere
.* Matches anything. No lookahead so this advances the "cursor"
$ Matches the end of the string
(?=.*[A-Za-z])
(?=.*[0-9])
.{8,32}
^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[@#$%^&+=]).{8,32}$
((?=.*[A-Z])|(?=.*[a-z]))(?=.*[0-9]).{8,32}
((?=.*[A-Z])|(?=.*[a-z]))(?=.*[@#$%^&+=]).{8,32}
(?=.*[0-9])(?=.*[@#$%^&+=]).{8,32}
(((?=.*[A-Z])|(?=.*[a-z]))(?=.*[0-9])).{8,32}|((?=.*[A-Z])|(?=.*[a-z]))(?=.*[@#$%^&+=]).{8,32}|(?=.*[0-9])(?=.*[@#$%^&+=]).{8,32}
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
// Passwords
String[] passwords = new String[8];
passwords[0] = "ABCabc123"; // letters (uppercase or lowercase) and numbers : VALID
passwords[1] = "*+uu*+aa"; // letters (uppercase or lowercase) and special characters : VALID
passwords[2] = "123+*$0*"; // numbers and special characters : VALID
passwords[3] = "ABCabc123*$#"; // letters (uppercase or lowercase) and numbers and special characters : VALID
passwords[4] = "ABCDabcd"; // letters (uppercase and lowercase) : INVALID
passwords[5] = "abcdefg"; // letters (lowercase) : INVALID
passwords[6] = "ABCDEFGH"; // letters (uppercase) : INVALID
passwords[7] = "123456789"; // numbers : INVALID
// Password validation pattern
var Validation = new Regex(@"((?=.*[A-Z])|(?=.*[a-z]))(?=.*[0-9]).{8,32}|((?=.*[A-Z])|(?=.*[a-z]))(?=.*[@#$%^&+=]).{8,32}|(?=.*[0-9])(?=.*[@#$%^&+=]).{8,32}");
// Validat the passwords
for (int i = 0; i < passwords.Length; i++){
var isValidated = Validation.IsMatch(passwords[i]);
Console.WriteLine(isValidated);
}
}
}
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
// Passwords
String[] passwords = new String[8];
passwords[0] = "ABCabc123"; // letters (uppercase or lowercase) and numbers : VALID
passwords[1] = "*+uu*+aa"; // letters (uppercase or lowercase) and special characters : VALID
passwords[2] = "123+*$0*"; // numbers and special characters : VALID
passwords[3] = "ABCabc123*$#"; // letters (uppercase or lowercase) and numbers and special characters : VALID
passwords[4] = "ABCDabcd"; // letters (uppercase and lowercase) : INVALID
passwords[5] = "abcdefg"; // letters (lowercase) : INVALID
passwords[6] = "ABCDEFGH"; // letters (uppercase) : INVALID
passwords[7] = "123456789"; // numbers : INVALID
// Password validation pattern
var hasLetterAndNumber = new Regex(@"((?=.*[A-Z])|(?=.*[a-z]))(?=.*[0-9]).{8,32}");
var hasLetterAndCharacter = new Regex(@"((?=.*[A-Z])|(?=.*[a-z]))(?=.*[@#$%^&+=]).{8,32}");
var hasNumberAndCharacter = new Regex(@"(?=.*[0-9])(?=.*[@#$%^&+=]).{8,32}");
// Validat the passwords
for (int i = 0; i < passwords.Length; i++){
if((hasLetterAndNumber.IsMatch(passwords[i])) || (hasLetterAndCharacter.IsMatch(passwords[i])) || (hasNumberAndCharacter.IsMatch(passwords[i]))){
Console.WriteLine(passwords[i]);
Console.WriteLine("\n");
}
}
}
}