簡體   English   中英

正則表達式,用於字母數字,至少1個數字和特殊字符

[英]Regex for alphanumeric, at least 1 number and special chars

我試圖找到一個正則表達式,它將給我以下驗證:

字符串應至少包含1位數字和至少1個特殊字符。 允許字母數字。

我嘗試了以下操作,但失敗了:

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

我嘗試了“ password1 $”,但是失敗了,我也嘗試了“ Password1!”。 但這也失敗了。

想法?

更新需要該解決方案才能與C#一起使用-當前,截至2013年10月22日發布的建議似乎無效。

嘗試這個:

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)零寬度的正向后斷言

有時一次只要做一個步驟簡單得多。 靜態構造函數從簡單的允許的特殊字符列表中構建轉義的字符類字符。 內置的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;
    }
}

這應該做的工作

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

用javascript測試過,不確定c#,可能需要一些調整。

它所做的是使用預期的正向查找來查找密碼的必需元素。

編輯

正則表達式旨在測試是否存在匹配項。 由於所有模式都是超前的,因此不會捕獲任何實際字符,並且匹配項為空,但是如果表達式“ match”,則密碼有效。

但是,由於問題是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);
    }

在行首添加末尾加.*$ ,如果密碼有效,表達式將匹配。 匹配值將是密碼。 (我猜)

這可能是可行的,可能有兩種,特殊字符前的數字或特殊字符后的數字。 您應該使用DOTALL(點所有字符)

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

這對我有用:

@“;:|((= ^ {8,} $?[@#$%\\ ^&*()_- + = [{]} <> ./一個-ZA-Z \\ d!?])? =([!@#$%\\ ^&*()_- + = [{]};:<> | ./?a-zA-Z \\ d] \\ W +){1,})(?= [ ^ 0-9] [0-9])[!@#$%\\ ^&*()_- + = [{]};:<> | ./?a-zA-Z \\ d] * $”

字母數字,至少1個數字和特殊字符,最小長度為8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM