【问题标题】:Password regular expression with character, number and punctuation带有字符、数字和标点符号的密码正则表达式
【发布时间】:2014-08-20 09:44:42
【问题描述】:

我需要一个密码的正则表达式,其中条件类似于

1.密码必须至少有 7 个但不超过 10 个字符。

2。密码必须至少包含一个大写字母,一个小写字母 大写字母、一位数字和一个标点符号。(例如,a-z, A-Z,0-9,!@#$%^&*()_+|~-=`{}[]:";'?,./)

我尝试使用:^(?=.{7,10})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+|~={}:";'?,./-][])$`

但标点部分解释不清楚,我在http://regex101.com/测试时

标点符号解释似乎坏了,可以解释一下原因并帮助我完成这个。

【问题讨论】:

  • 你的意思是regex101.com/r/qE6oS6/8
  • 您限制 10 个字符的任何充分理由?...许多有安全意识的人将拥有/想要一个 12、14、16+ 个字符的密码。
  • @scunliffe 好点,谢谢你,我们会讨论你的观点
  • @Suganthan 因为您正在对密码进行散列(和盐渍(希望!!)),所以应该没有理由限制它的上限字符约束。
  • @stuXnet 我没明白你的意思,你能说得简单点吗。

标签: java regex


【解决方案1】:

以下应该适合您的需要:

^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+|~=`{}\[\]:";'<>?,./-]).{7,10}$

Debuggex Demo

不要忘记在 Java 中转义反斜杠,因为在字符串文字中。换句话说,将上述正则表达式中的每个反斜杠替换为两个反斜杠。

如果您也不想允许空格,只需将.{7,10} 替换为\S{7,10}(同样,在Java 中转义反斜杠)。


你的错误的两个原因:

  • 您没有转义 punct 类中的 [] 字符(语法错误);
  • 您必须将- 字符放在字符类的开头或结尾。否则,它将被解释为特殊字符。例如,[A-Z] 匹配AZ 之间的任何大写字符,而[AZ-] 匹配AZ-

【讨论】:

  • 用于测试字符串(1:- 78tre48`12, 2:- yuuyuyuyuyuy, 3:- ad8\\\09 +9) 它实际上失败了
  • 可能这是修正版,请检查我是否正确/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&amp;*()_+|~={}[]:";'?,.\/-])。{7, 10}$`
  • @Suganthan 在这些字符串中,您没有大写字母,而您要求的密码 [that] 必须至少包含一个大写字母
  • 对,你是对的,让我清楚地测试一下我的“测试字符串和我写的代码”
【解决方案2】:

你根本不消耗你的字符串。添加

.* 

在最后消耗它。

【讨论】:

    【解决方案3】:

    检查这个

              ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})
    

    练习

               http://www.regexr.com/
    

    【讨论】:

    • 为什么是{6,20}? OP 规定:密码必须至少有 7 个但不超过 10 个字符
    • ya.. 这是一个例子......第一个参数是,必须至少有 6 个字符,第二个参数......不应超过 20 个字符,您可以相应地更改它。跨度>
    【解决方案4】:

    我很想写这个正则表达式:

    为方便阅读,只需使用!@#作为特殊字符,!@#后面可以添加其他字符:

    ^(?!\d+$)(?![a-z]+$)(?![A-Z]+$)(?![!@#]+$)(?![a-z\d]+$)(?![A-Z\d]+$)(?![!@#\d]+$)(?![a-zA-Z]+$)(?![!@#a-z]+$)(?![!@#A-Z]+$)(?![a-zA-Z\d]+$)(?![!@#a-z\d]+$)(?![!@#A-Z\d]+$)(?![!@#a-zA-Z]+$)[!@#a-zA-Z\d]{7,10}$

    DEMO:

    ^           
    (?!\d+$)           // the password cannot be all digites,of course
    (?![a-z]+$)        // the password cannot be all a-z
    (?![A-Z]+$)        // the password cannot be all A-Z
    (?![!@#]+$)        // the password cannot be all !@# 
    (?![a-z\d]+$)      // the password cannot be all a-z 0-9
    (?![A-Z\d]+$)      // the password cannot be all A-Z 0-9
    (?![!@#\d]+$)      // the password cannot be all !@# 0-9
    (?![a-zA-Z]+$)     // the password cannot be all A-Z a-z
    (?![!@#a-z]+$)     // the password cannot be all !@# a-z
    (?![!@#A-Z]+$)     // the password cannot be all !@# A-Z
    (?![a-zA-Z\d]+$)   // the password cannot be all A-Z a-z 0-9
    (?![!@#a-z\d]+$)   // the password cannot be all !@# a-z 0-9
    (?![!@#A-Z\d]+$)   // the password cannot be all !@# A-Z
    (?![!@#a-zA-Z]+$)  // the password cannot be all !@# a-z A-Z
    [!@#a-zA-Z\d]{7,10}  // the password SHOULD be in `!@# a-z A-Z 0-9` and length 7-10
    $
    

    【讨论】:

    • :O 解释一下,请.. :)
    • @1337 更新我的解释。 BTW:为方便阅读,只需使用!@#作为特殊字符,!@#后面可以添加其他字符
    【解决方案5】:

    这里也不错

    ([a-zA-Z0-9!@#$%^&*_+=(){}[\];':",\.<>\/\?-]{8,38})
    

    也用于测试我喜欢https://regex101.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      相关资源
      最近更新 更多