正则表达式验证密码

时间:2016-06-18 00:46:55

标签: regex validation passwords

我需要一个正则表达式来验证密码。约束是:

- 最大嵌入空间:0

- 最小长度:8

- 最大长度:8

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

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

-Maximum Repetitive Character / Number:2

- 最大顺序字符:3

- 最大特殊字符:0

- 最小字母:1

- 最小数字:1

不确定我应该怎么做,是采取消极或积极的方法。

1 个答案:

答案 0 :(得分:3)

描述

^(?!.*?([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不好
  5. 注意:此表达式需要使用不区分大小写的标志才能匹配大小写值。

    实施例

    现场演示

    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"
    ----------------------------------------------------------------------