2017-06-02 274 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]*$ 
相关问题