我复制了一个在 Javascript 中工作的 RegEx。但是当我在 C# 中运行它时,它返回 false。我不确定是我的代码不正确还是正则表达式。这是我的代码。
bool isValid = true;
string nameInput = "Andreas Johansson";
string emailInput = "email@gmail.com";
string passwordInput = "abcABC123";
string namePattern = @"^[A-z]+(-[A-z]+)*$";
string emailPattern = @"^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$";
string passwordPattern = @"^(?=.*\d+)(?=.*[a-zA-Z])[0-9a-zA-Z!@#$%]{6,50}$";
Regex nameRegEx = new Regex(namePattern);
Regex emailRegEx = new Regex(emailPattern);
Regex passwordRegEx = new Regex(passwordPattern);
if (model.CreateFullName.Length < 3 || !nameRegEx.IsMatch(nameInput))
isValid = false;
if (model.CreateEmail.Length < 3 || !emailRegEx.IsMatch(emailInput))
isValid = false;
if (model.CreatePassword.Length < 3 || !passwordRegEx.IsMatch(passwordInput))
isValid = false;
感谢您的投入!