【问题标题】:regex password with min 8 characters, upper and lower Case, and no special character at Beginning and end [duplicate]正则表达式密码,最少 8 个字符,大小写,开头和结尾没有特殊字符 [重复]
【发布时间】:2021-02-16 16:48:47
【问题描述】:
^[a-zA-Z0-9](?=.+[a-z])(?=.+[A-Z])(?=.*[0-9])(?=.+[-+_!@#$%^&*.,?])(\\S*[a-zA-Z0-9]){8,}?$

这是我的密码正则表达式。它很不稳定,并且始终无法正常工作,例如:如果我尝试 ABcAcs!12,它就无法正常工作。

这是我的家庭项目的任务。 密码应该:

  • 至少有 8 个字符,
  • 最少 1 个大小写字母,
  • 至少 1 个特殊字符
  • 特殊字符不允许出现在开头或结尾 字符串。

我觉得我需要彻底检查它,但我希望你能帮助我。

【问题讨论】:

标签: java regex passwords


【解决方案1】:
  • 问题是模式以匹配此字符类[a-zA-Z0-9]中的1个字符开头。

    在模式的末尾,这部分(\S*[a-zA-Z0-9]){8,}必须至少匹配[a-zA-Z0-9]之一的8次,并且在字符串ABcAcs!12中有7个匹配,分别是ABcAcs和@987654327 @

  • 另请注意,其中 2 个前瞻以 .+ 而不是 .* 开头,这意味着它们不考虑后面的第一个字符。

    这个9A9a$adfa 应该也是有效的,但是由于.+ 它错过了A

  • 由于您希望最小为 8,并且您已经匹配了第一个字符,所以末尾的量词应该是 {7,}

最终的模式,使用否定字符类来防止回溯可能是

^[a-zA-Z0-9](?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*[0-9])(?=[^-+_!@#$%^&*.,?]*[-+_!@#$%^&*.,?])[-+_!@#$%^&*.,?a-zA-Z0-9]{7,}$

Regex demo

【讨论】:

    【解决方案2】:

    两种略有不同的方法:

    正则表达式 [1]

    ^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[-+_!@#$%^&*.,?])[a-zA-Z\d][a-zA-Z\d\-+_!@#$%^&*.,?]{8,}[a-zA-Z\d]$
    
    ^                               // Matches the start of the string
    (?=.*[A-Z])                     // Positive lookahead to cheack for presence of a uppercase letter
    (?=.*[a-z])                     // Positive lookahead to cheack for presence of an lowercase letter
    (?=.*\d)                        // Positive lookahead to cheack for presence of a number
    (?=.*[-+_!@#$%^&*.,?])          // Positive lookahead to cheack for presence of a special character letter
    [a-zA-Z\d]                      // Matches a "non-special" character at the start of the string
    [a-zA-Z\d\-+_!@#$%^&*.,?]{7,}   // Matche any allowed character 7+ times
    [a-zA-Z\d]                      // Makes sure the last (minimum 8th) character isn't special
    $                               // Matches the end of the string
    

    正则表达式 [2]

    ^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=[a-zA-Z\d].*[-+_!@#$%^&*.,?].*[a-zA-Z\d]$)[a-zA-Z\d\-+_!@#$%^&*.,?]{8,}$
    
    ^                                               // Matches the start of the string
    (?=.*[A-Z])                                     // Positive lookahead to cheack for presence of a uppercase letter
    (?=.*[a-z])                                     // Positive lookahead to cheack for presence of an lowercase letter
    (?=.*\d)                                        // Positive lookahead to cheack for presence of a number
    (?=[a-zA-Z\d].*[-+_!@#$%^&*.,?].*[a-zA-Z\d]$)   // Positive lookahead to check that the string starts and ends with a letter or number but contains a special character
    [a-zA-Z\d\-+_!@#$%^&*.,?]{8,}                   // Matches any allowed character 8+ times
    $                                               // Matches the end of the string
    

    输出

    _Password               [1]         [2]
    ---------------------------------------
    abcdef                   0   =>      0
    abcDEF                   0   =>      0
    abcDEF789_               0   =>      0
    _123DEF789               0   =>      0
    abc_DEF123               1   =>      1
    abc_D1                   0   =>      0
    abc%defghi               0   =>      0
    some pass                0   =>      0
    Some Other Pass_1234     0   =>      0
    alpha_Numeric$12         1   =>      1
    Random_1#@!S             1   =>      1
    RRARaa???23              1   =>      1
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2019-06-05
      • 2017-07-06
      • 1970-01-01
      • 2018-06-27
      • 2014-01-29
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      相关资源
      最近更新 更多