密码验证的正则表达式

16 浏览
0 Comments

密码验证的正则表达式

这个问题已经得到了回答:

密码必须至少包含八个字符,至少一个数字,大写字母、小写字母和特殊字符的正则表达式

我正在寻找一个正则表达式来验证以下密码:

  • 密码必须至少有7个字符,最多50个字符
  • 密码必须包含以下四个类别中至少三个类别中的字符:

    1. 英文大写字母(A-Z)
    2. 英文小写字母(a-z)
    3. 十进制数字(0-9)
    4. 非字母数字字符(例如,!$#,%)

我尝试了以下方法:

^.*(?=.{7,50})(?=.*\d)(?=.*[A-Z]).*$

这不检查特殊字符,并且我不知道如何使密码必须包含来自以下四个类别中至少三个类别中的字符。

有人能帮忙建议一个正则表达式来验证这个密码策略吗?

admin 更改状态以发布 2023年5月24日
0
0 Comments

为了在一个正则表达式中添加多个条件,我们使用管道符或括号 /(regex1)|(regex2)|(regex2)/ 或 (regex1)(regex2)。\n\n每一个正则表达式条件都很基础,而且对于你提出的每个条件,都有很多例子。\n\n你可以参考这里:\n密码必须包含至少8个字符、至少1个数字、大小写字母和特殊字符的正则表达式

0
0 Comments

描述

^(?:(?=.*?[A-Z])(?:(?=.*?[0-9])(?=.*?[-!@#$%^&*()_[\]{},.<>+=])|(?=.*?[a-z])(?:(?=.*?[0-9])|(?=.*?[-!@#$%^&*()_[\]{},.<>+=])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%^&*()_[\]{},.<>+=]))[A-Za-z0-9!@#$%^&*()_[\]{},.<>+=-]{7,50}$

正则表达式可视化

为了更好地查看图片,你可以右键点击图片,选择在新窗口中查看。

该正则表达式将执行以下操作:

  • 要求字符串长度为7至50个字符
  • 允许字符串包含A-Za-z0-9!@#$%^&*()_[\]{},.<>+=-字符
  • 要求至少有一个字符符合以下三种情况之一
    1. 英文字母大写字符A-Z
    2. 英文字母小写字符a-z
    3. 十进制数字0-9
    4. 非字母数字字符!@#$%^&*()_[]{},.<>+=-

示例

实时演示

https://regex101.com/r/jR9cC7/1

示例文本

         1         2         3         4         5        6
12345678901234567890123456789012345678901234567890124567890
aaaaAAAA1111
aaaaBBBBBBB
AAAAaaaa__
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!AA

允许的字符串

aaaaAAAA1111
AAAAaaaa__
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    (?=                      look ahead to see if there is:
----------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
----------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
----------------------------------------------------------------------
        .*?                      any character except \n (0 or more
                                 times (matching the least 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 least amount
                                 possible))
----------------------------------------------------------------------
        [-                       any character of: '-', '!', '@',
        !@#$%^&*()_[\]           '#', '$', '%', '^', '&', '*', '(',
        {},.<>+=]                ')', '_', '[', '\]', '{', '}', ',',
                                 '.', '<', '>', '+', '='
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
----------------------------------------------------------------------
        .*?                      any character except \n (0 or more
                                 times (matching the least amount
                                 possible))
----------------------------------------------------------------------
        [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        (?=                      look ahead to see if there is:
----------------------------------------------------------------------
          .*?                      any character except \n (0 or more
                                   times (matching the least amount
                                   possible))
----------------------------------------------------------------------
          [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
        )                        end of look-ahead
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        (?=                      look ahead to see if there is:
----------------------------------------------------------------------
          .*?                      any character except \n (0 or more
                                   times (matching the least amount
                                   possible))
----------------------------------------------------------------------
          [-                       any character of: '-', '!', '@',
          !@#$%^&*()_[             '#', '$', '%', '^', '&', '*', '(',
          \]{},.<>+=]              ')', '_', '[', '\]', '{', '}',
                                   ',', '.', '<', '>', '+', '='
----------------------------------------------------------------------
        )                        end of look-ahead
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (?=                      look ahead to see if there is:
----------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least 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 least 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 least amount
                               possible))
----------------------------------------------------------------------
      [-                       any character of: '-', '!', '@', '#',
      !@#$%^&*()_[\]{}         '$', '%', '^', '&', '*', '(', ')',
      ,.<>+=]                  '_', '[', '\]', '{', '}', ',', '.',
                               '<', '>', '+', '='
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  [A-Za-z0-                any character of: 'A' to 'Z', 'a' to 'z',
  9!@#$%^&*()_[\]{},.<     '0' to '9', '!', '@', '#', '$', '%', '^',
  >+=-]{7,50}              '&', '*', '(', ')', '_', '[', '\]', '{',
                           '}', ',', '.', '<', '>', '+', '=', '-'
                           (between 7 and 50 times (matching the most
                           amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------

0