【问题标题】:Regex pass validation正则表达式通过验证
【发布时间】:2012-01-08 08:00:26
【问题描述】:

我想知道如何使用正则表达式验证密码。

我尝试使用lookahaed 断言,但不知何故失败了。不知道怎么匹配一定长度的字符串。

我的条件是:

  • 至少 8 个字符
  • 至少一位数
  • 至少一个英文小写字母
  • 至少一个大写英文字母
  • 至少有一个符号!@#$%-
  • 只允许使用以上字符

提前致谢!

【问题讨论】:

    标签: .net regex validation passwords


    【解决方案1】:

    你在前瞻断言的正确步骤。

    您的要求可以细分为:

    • (?=.{8,})
    • (?=.*\d)
    • (?=.*[a-z])
    • (?=.*[A-Z])
    • (?=.*[!@#$%-])
    • [\da-zA-Z!@#$%-]*

    把它们放在一起,你最终会得到这个:

    foundMatch = Regex.IsMatch(subjectString, @"^(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%-])[\da-zA-Z!@#$%-]*$");
    

    这可以反过来解释:

    "
    ^                   # Assert position at the beginning of the string
    (?=                 # Assert that the regex below can be matched, starting at this position (positive lookahead)
       .                   # Match any single character that is not a line break character
          {8,}                # Between 8 and unlimited times, as many times as possible, giving back as needed (greedy)
    )
    (?=                 # Assert that the regex below can be matched, starting at this position (positive lookahead)
       .                   # Match any single character that is not a line break character
          *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       \d                  # Match a single digit 0..9
    )
    (?=                 # Assert that the regex below can be matched, starting at this position (positive lookahead)
       .                   # Match any single character that is not a line break character
          *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       [a-z]               # Match a single character in the range between “a” and “z”
    )
    (?=                 # Assert that the regex below can be matched, starting at this position (positive lookahead)
       .                   # Match any single character that is not a line break character
          *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       [A-Z]               # Match a single character in the range between “A” and “Z”
    )
    (?=                 # Assert that the regex below can be matched, starting at this position (positive lookahead)
       .                   # Match any single character that is not a line break character
          *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
       [!@#$%-]            # Match a single character present in the list below
                              # One of the characters “!@#$”
                              # The character “%”
                              # The character “-”
    )
    [\da-zA-Z!@#$%-]    # Match a single character present in the list below
                           # A single digit 0..9
                           # A character in the range between “a” and “z”
                           # A character in the range between “A” and “Z”
                           # One of the characters “!@#$”
                           # The character “%”
                           # The character “-”
       *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    $                   # Assert position at the end of the string (or before the line break at the end of the string, if any)
    "
    

    【讨论】:

      【解决方案2】:

      这应该适合你

      ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%-]).{8,}$
      

      解释

      NODE                     EXPLANATION
      --------------------------------------------------------------------------------
        ^                        the beginning of the string
      --------------------------------------------------------------------------------
        (?=                      look ahead to see if there is:
      --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more times
                                   (matching the most amount possible))
      --------------------------------------------------------------------------------
          [0-9]                    any character of: '0' to '9'
      --------------------------------------------------------------------------------
        )                        end of look-ahead
      --------------------------------------------------------------------------------
        (?=                      look ahead to see if there is:
      --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more times
                                   (matching the most amount possible))
      --------------------------------------------------------------------------------
          [a-z]                    any character of: 'a' to 'z'
      --------------------------------------------------------------------------------
        )                        end of look-ahead
      --------------------------------------------------------------------------------
        (?=                      look ahead to see if there is:
      --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more times
                                   (matching the most amount possible))
      --------------------------------------------------------------------------------
          [A-Z]                    any character of: 'A' to 'Z'
      --------------------------------------------------------------------------------
        )                        end of look-ahead
      --------------------------------------------------------------------------------
        (?=                      look ahead to see if there is:
      --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more times
                                   (matching the most amount possible))
      --------------------------------------------------------------------------------
          [!@#$%-]                 any character of: '!', '@', '#', '$',
                                   '%', '-'
      --------------------------------------------------------------------------------
        )                        end of look-ahead
      --------------------------------------------------------------------------------
        .{8,}                    any character except \n (at least 8 times
                                 (matching the most amount possible))
      --------------------------------------------------------------------------------
        $                        before an optional \n, and the end of the
                                 string
      

      确实,您不应该强制使用这种密码。请参阅这部值得注意的 xkcd 漫画,了解原因

      经过 20 年的努力,我们已成功训练每个人使用人类难以记住但计算机容易猜测的密码。

      【讨论】:

      • 虽然这是正确的,但我想你忘记了最后的要求。
      • 谢谢!顺便说一句好漫画!
      【解决方案3】:

      您是否有任何理由需要对所有这些规则使用单个正则表达式?单独测试每一个更常见,也更易读。例如:

      if(pass.length < 8 || ! pass.matches(/[0-9]/) || ! pass.matches(/[a-z]/)
         || ! pass.matches(/[A-Z]/) || ! pass.matches(/[!@#$%-]/)
         || pass.matches(/[^0-9A-Za-z!@#$%-]/))
      {
          . . . // reject password
      }
      

      【讨论】:

      • 我的需求规格是正则表达式...谢谢!
      【解决方案4】:

      大概是这样的:
      (糟糕,最好让这至少正确)
      扩展

      ^
      (?=[0-9a-zA-Z!@#$%-]{8,}$)
      (?=.*[0-9])
      (?=.*[a-z])
      (?=.*[A-Z])
      (?=.*[!@#$%-])
      

      【讨论】:

        猜你喜欢
        • 2015-04-20
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 2011-07-01
        • 2012-10-05
        • 1970-01-01
        • 2021-05-07
        相关资源
        最近更新 更多