我需要一个正则表达式来验证密码。限制条件是:
-最大嵌入空间: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}$
此正则表达式被分成将执行以下操作的组件:
-
^[a-pr-y0-9]{8}$
- 不允许有空格
- 需要 8 个字符,不多不少
- 不允许使用字母:
Q
、q
、Z
、z
- 允许零个特殊字符
-
(?!.*?([a-z0-9])(?:.*?\1){2})
- 允许任何字符在字符串中的任意位置最多出现 2 次li>
-
(?=.*?[a-z])
- 需要至少 1 个字母
-
(?=.*?[0-9])
- 要求至少 1 位数字
(?!.*?(?: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
不好
- 最多允许 3 个连续字符,
注意:此表达式需要使用不区分大小写的标志,以便匹配大小写值。
示例
现场演示
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/