正则表达式为字母数字,至少1个数字和特殊字符

时间:2013-10-22 06:16:10

标签: c# regex

我正在尝试找到一个正则表达式,它将给我以下验证:

字符串应包含至少1位数字和至少1个特殊字符。是否允许使用字母数字。

我尝试了以下但是失败了:

@"^[a-zA-Z0-9@#$%&*+\-_(),+':;?.,!\[\]\s\\/]+$]"

我尝试了“password1 $”,但失败了 我也试过“Password1!”但那也失败了。

想法?

更新 需要使用C#的解决方案 - 目前截至2013年10月22日发布的建议似乎不起作用。

5 个答案:

答案 0 :(得分:4)

试试这个:

Regex rxPassword = new Regex( @"
  ^               # start-of-line, followed by
  [a-zA-Z0-9!@#]+ # a sequence of one or more characters drawn from the set consisting of ASCII letters, digits or the punctuation characters ! @ and #
  (<=[0-9])       # at least one of which is a decimal digit
  (<=[!@#])       # at least one of which is one of the special characters
  (<=[a-zA-Z])    # at least one of which is an upper- or lower-case letter
  $               # followed by end-of-line
" , RegexOptions.IgnorePatternWhitespace ) ;

构造(<=regular-expression)是一个零宽度正面后视断言

答案 1 :(得分:1)

有时,只需一步一步地执行操作就可以轻松实现很多。静态构造函数从允许的特殊字符的简单列表构建转义字符类字符。内置的Regex.Escape方法在这里不起作用。

public static class PasswordValidator {

    private const string ALLOWED_SPECIAL_CHARS = @"@#$%&*+_()':;?.,![]\-";
    private static string ESCAPED_SPECIAL_CHARS;

    static PasswordValidator() {
        var escapedChars = new List<char>();
        foreach (char c in ALLOWED_SPECIAL_CHARS) {
            if (c == '[' || c == ']' || c == '\\' || c == '-')
                escapedChars.AddRange(new[] { '\\', c });
            else
                escapedChars.Add(c);
        }
        ESCAPED_SPECIAL_CHARS = new string(escapedChars.ToArray());
    }

    public static bool IsValidPassword(string input) {
        // Length requirement?
        if (input.Length < 8) return false;

        // First just check for a digit
        if (!Regex.IsMatch(input, @"\d")) return false;

        // Then check for special character
        if (!Regex.IsMatch(input, "[" + ESCAPED_SPECIAL_CHARS + "]")) return false;

        // Require a letter?
        if (!Regex.IsMatch(input, "[a-zA-Z]")) return false;

        // DON'T allow anything else:
        if (Regex.IsMatch(input, @"[^a-zA-Z\d" + ESCAPED_SPECIAL_CHARS + "]")) return false;

        return true;
    }
}

答案 2 :(得分:0)

这应该做的工作

(?:(?=.*[0-9]+)(?=.*[a-zA-Z]+)(?=.*[@#$%&*+\-_(),+':;?.,!\[\]\s\\/]+))+

使用javascript测试,不确定c#,可能需要一些调整。

它的作用是使用预期的积极前瞻来找到所需的密码元素。

修改

正则表达式旨在测试是否存在匹配项。由于所有模式都是前瞻性的,因此不会捕获真实字符并且匹配为空,但如果表达式“匹配”,则密码有效。

但是,因为问题是C#(对不起,我不知道c#,只是即兴创作和调整样本)

    string input = "password1!";
    string pattern = @"^(?:(?=.*[0-9]+)(?=.*[a-zA-Z]+)(?=.*[@#$%&*+\-_(),+':;?.,!\[\]\s\\/]+))+.*$";
    Regex rgx = new Regex(pattern, RegexOptions.None);

    MatchCollection matches = rgx.Matches(input);
   if (matches.Count > 0) {
      Console.WriteLine("{0} ({1} matches):", input, matches.Count);
      foreach (Match match in matches)
         Console.WriteLine("   " + match.Value);
    }

添加行首和.*$到最后,如果密码有效,表达式将匹配。并且匹配值将是密码。 (我猜)

答案 3 :(得分:0)

这可能是有效的,有两种可能,特殊字符之前的数字或特殊字符之后的数字。你应该使用DOTALL(点所有字符)

^((.*?[0-9].*?[@#$%&*+\-_(),+':;?.,!\[\]\s\\/].*)|(.*?[@#$%&*+\-_(),+':;?.,!\[\]\s\\/].*?[0-9].*))$

答案 4 :(得分:0)

这对我有用:

@“(= ^ [@#$%\ ^&安培; *()_- + = [{]};:!?&LT;&GT; | ./一个-ZA-Z \ d] {8 ,} $)(=([@#$%\ ^&安培; *()_- + = [{]};:!?&LT;&GT; | ./一个-ZA-Z \ d] \ W +){1,})(= [^ 0-9] [0-9])[@#$%\ ^&安培;!*()_- + = [{]} ;: &LT;&GT; |?./一个-ZA-Z \ d] * $“

字母数字,至少1个数字和特殊字符,最小长度为8

相关问题