正则表达式验证密码

标签 regex validation passwords

我需要一个正则表达式来验证密码。限制条件是:

-最大嵌入空间:0

-最小长度:8

-最大长度:8

-不得包含字母:Q、q、Z、z

-一个字符/数字最多出现次数:2

-最大重复字符/数字:2

-最大连续字符:3

-最大特殊字符:0

-最小字母:1

-最小数量:1

不确定我应该如何解决这个问题,是采取消极还是积极的方法。

最佳答案

描述

^(?!.*?([a-z0-9])(?:.*?\1){2})(?=.*?[a-z])(?=.*?[0-9])(?!.*?(?:0(?:123|987)|1234|2345|3(?:456|210)|4(?:567|321)|5(?:678|432)|6(?:789|543)|7(?:890|654)|8765|9876))(?!.*?(?:abcd|bcde|cdef|d(?:efg|cba)|e(?:fgh|dcb)|f(?:ghi|edc)|g(?:hij|fed)|h(?:ijk|gfe)|i(?:jkl|hgf)|j(?:klm|ihg)|k(?:lmn|jih)|l(?:mno|kji)|m(?:nop|lkj)|nmlk|onml|ponm|rstu|stuv|tuvw|u(?:vwx|tsr)|v(?:wxy|uts)|w(?:vut)|xwvu|yxwv))[a-pr-y0-9]{8}$

Regular expression visualization

此正则表达式被分成将执行以下操作的组件:

  1. ^[a-pr-y0-9]{8}$
    • 不允许有空格
    • 需要 8 个字符,不多不少
    • 不允许使用字母: QqZz
    • 允许零个特殊字符
  2. (?!.*?([a-z0-9])(?:.*?\1){2})
    • 允许任何字符在字符串中的任意位置最多出现 2 次
    • (?=.*?[a-z])
    • 需要至少 1 个字母
  3. (?=.*?[0-9])
    • 要求至少 1 位数字
  4. (?!.*?(?:0(?:123|987)|1234|2345|3(?:456|210)|4(?:567|321)|5(?:678|432)|6(?:789|543)|7(?:890|654)|8765|9876))(?!.*?(?:abcd|bcde|cdef|d(?:efg|cba)|e(?:fgh|dcb)|f(?:ghi|edc)|g(?:hij|fed)|h(?:ijk|gfe)|i(?:jkl|hgf)|j(?:klm|ihg)|k(?:lmn|jih)|l(?:mno|kji)|m(?:nop|lkj)|nmlk|onml|ponm|rstu|stuv|tuvw|u(?:vwx|tsr)|v(?:wxy|uts)|w(?:vut)|xwvu|yxwv))

    • 最多允许 3 个连续字符,abc 可以,但 abcd 不好
    • 最多允许 3 个反向连续字符,cba 可以,但 dcba 不好

注意:此表达式需要使用不区分大小写的标志,以便匹配大小写值。

示例

现场演示

https://regex101.com/r/gL9jN5/4

示例文本

12345678
asfkd2ls
abcd12js
ibicid13
aaaafd212
fdadms1

示例匹配

asfkd2ls

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      [a-z0-9]                 any character of: 'a' to 'z', '0' to
                               '9'
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    (?:                      group, but do not capture (2 times):
----------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
----------------------------------------------------------------------
      \1                       what was matched by capture \1
----------------------------------------------------------------------
    ){2}                     end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      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 not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      0                        '0'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        123                      '123'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        987                      '987'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      1234                     '1234'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      2345                     '2345'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      3                        '3'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        456                      '456'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        210                      '210'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      4                        '4'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        567                      '567'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        321                      '321'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      5                        '5'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        678                      '678'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        432                      '432'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      6                        '6'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        789                      '789'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        543                      '543'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      7                        '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        890                      '890'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        654                      '654'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      8765                     '8765'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      9876                     '9876'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      abcd                     'abcd'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      bcde                     'bcde'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      cdef                     'cdef'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      d                        'd'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        efg                      'efg'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        cba                      'cba'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      e                        'e'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        fgh                      'fgh'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        dcb                      'dcb'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      f                        'f'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        ghi                      'ghi'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        edc                      'edc'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      g                        'g'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        hij                      'hij'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        fed                      'fed'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      h                        'h'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        ijk                      'ijk'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        gfe                      'gfe'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      i                        'i'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        jkl                      'jkl'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        hgf                      'hgf'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      j                        'j'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        klm                      'klm'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        ihg                      'ihg'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      k                        'k'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        lmn                      'lmn'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        jih                      'jih'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      l                        'l'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        mno                      'mno'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        kji                      'kji'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      m                        'm'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        nop                      'nop'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        lkj                      'lkj'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      nmlk                     'nmlk'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      onml                     'onml'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ponm                     'ponm'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      rstu                     'rstu'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      stuv                     'stuv'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      tuvw                     'tuvw'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      u                        'u'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        vwx                      'vwx'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        tsr                      'tsr'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      v                        'v'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        wxy                      'wxy'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        uts                      'uts'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      w                        'w'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        vut                      'vut'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      xwvu                     'xwvu'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      yxwv                     'yxwv'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  [a-pr-y0-9]{8}           any character of: 'a' to 'p', 'r' to 'y',
                           '0' to '9' (8 times)
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------

关于正则表达式验证密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37892215/

相关文章:

R - 正则表达式根据第一个点分隔字符串?

asp.net - Telerik rad 日期选择器在用户键入 future 日期时显示错误消息

security - 通过键空间而不是使用的字符类检查密码复杂性的好处?

passwords - Django : Reverse for 'password_reset_complete' not found

android - 在 TextInputLayout 中使用 endIconMode 默认情况下启用结束图标(可见图标)

regex - 如何使用 shell 脚本查找 Linux 发行版名称?

java - 正则表达式同时显示不同的返回不同的结果

php - 拉维尔 5 : SQLSTATE[23000]: Integrity constraint violation

java - 如何从 JSF 组件获取验证状态

c# - 搜索字符串中每个出现的子字符串 C#