2017-06-02 73 views
1

看來我堅持使用一個簡單的正則表達式來進行密碼檢查。正則表達式:任何字母,數字和0到3特殊字符

我想什麼:

  • 8到30個符號(總)
  • 與任何這些:[A-Za-z\d]
  • 0到的這3個[ -/:[email protected][-`{-~À-ÿ](特別名單)

我接過來一看here,然後我寫的東西,如:

(?=.{8,15}$)(?=.*[A-Za-z\d])(?!([ -\/:[email protected][-`{-~À-ÿ])\1{4}).* 

But it doesn't work, one can put more than 3 of the special chars list

任何提示?

+2

我的提示是讓用戶儘可能多地使用他想要的特殊字符 –

+1

爲什麼你想要正則表達式?在計算字符數方面,這並不好。當然,你可以做到這一點,但這是一種痛苦,很難理解或維護。只需使用一些'IndexOf'或'String.Contains',你就完成了。 – HimBromBeere

+0

@RomanoZumbé沒有選擇。 – Szadek35

回答

0

洗牌張望了一下你的正則表達式後,它爲您提供the examples(我想你做與例如一個錯誤「A @〜`C:」,它不應該匹配爲它有6個特殊字符):

(?!.*(?:[ -\/:[email protected][-`{-~À-ÿ].*){4})^[A-Za-z\d -\/:[email protected][-`{-~À-ÿ]{8,30}$ 

它只需要一個先行而不是兩個,是因爲長度和字符集檢查可以在沒有先行完成:^ [A-ZA-Z \ d -/- @ [-` { - 〜À-ÿ] {8,30} $

我將負向預測改爲有點正確。你的錯誤是隻檢查連續的特殊字符,並且以一種使前瞻從未命中的方式插入通配符.*(因爲通配符允許所有內容)。

+0

哦,是的,我讓我誤以「A @〜'C:」爲例。對不起,我更新了[頁面](https://regex101.com/r/X9wH8D/4/)以解決它。感謝您的回答,它效果非常好!我通過否定實現規則「0到3」特殊字符的第一個前瞻來理解你的做法,然後檢查授權符號。 非常感謝:) 已解決。 – Szadek35

+0

@ Szadek35不客氣。我剛纔看到我忘記了前視中的一些括號。他們沒有必要,我刪除了他們。 –

0

這項工作?

  string characters = " -/:[email protected][-`{-~À-ÿ"; 
      string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
      string[] inputs = { 
       "AABBCCDD", 
       "aaaaaaaa", 
       "11111111", 
       "a1a1a1a1", 
       "[email protected]@@@AA", 
       "A1C EKFE", 
       "AADE F" 
           }; 

      foreach (string input in inputs) 
      { 
       var counts = input.Cast<char>().Select(x => new { ch = characters.Contains(x.ToString()) ? 1 : 0, letter = letters.Contains(x.ToString()) ? 1 : 0, notmatch = (characters + letters).Contains(x) ? 0 : 1}).ToArray(); 
       Boolean isMatch = (input.Length >= 8) && (input.Length <= 30) && (counts.Sum(x => x.notmatch) == 0) && (counts.Sum(x => x.ch) <= 3); 

       Console.WriteLine("Input : '{0}', Matches : '{1}'", input, isMatch ? "Match" : "No Match"); 
      } 
      Console.ReadLine(); 
+0

感謝您的幫助。我還發現在C#中我會更容易。 而且不需要大量的代碼,Regex.IsMatch()和Regex.Matches()。Count就足夠了:) – Szadek35

0

我會用:(如果你想堅持到正則表達式)

var specialChars = @" -\/:[email protected][-`{-~À-ÿ"; 
var regularChars = @"A-Za-z\d"; 
if (Regex.Match(password,$"^(.[{regularChars}{specialChars}]{7,29})$").Success && Regex.Matches(password, $"[{specialChars}]").Count<=3)) 
{ 
    //Password OK 
} 

如果包括:

  1. 檢查長度,如果密碼包含非法字符

  2. 檢查是否含有3次特殊字符

痘痘快:

var specialChars = @" -\/:[email protected][-`{-~À-ÿ"; 
var regularChars = @"A-Za-z\d"; 
var minChars = 8; 
var maxChars = 30; 
if (password.Length >= minChars && password.Length <= maxChars && Regex.Match(password,$"^[{regularChars}{specialChars}]+$").Success && Regex.Matches(password, $"[{specialChars}]").Count<=3)) 
{ 
    //Password OK 
} 
+0

感謝您的幫助,但第一個答案不起作用。 對於C#之一,我也想出了類似的東西,用長度> 7 && <31 :) – Szadek35

0

新手here..I覺得我已經成功地得到你所需要的,但你共享的測試案例之一是有點奇怪..

[email protected]~` C: 
    OK -- Match (3 specials, it's okay) 

這不應該失敗,因爲它有超過3特價?

你能試試嗎?如果有效,我會輸入正則表達式的解釋。

https://regex101.com/r/KCL6R1/2

(?=^[A-Za-z\d -\/:[email protected][-`{-~À-ÿ]{8,30}$)^(?:[A-Za-z\d]*[ -\/:[email protected][-`{-~À-ÿ]){0,3}[A-Za-z\d]*$ 
相關問題