我试图得到一个正则表达式,它检查以确保提供的int是4位数,并且它不是顺序的,也不包含所有重复数字(也适用于3位数),无论是按升序还是降序。我真的不在乎正则表达式是否返回非允许数字的匹配项,或者如果允许则返回原始数字的匹配项。
因此,例如,所有这些数字都是我不需要通过正则表达式进行验证的原因:
虽然这些数字会通过:
换句话说,
感谢。
答案 0 :(得分:3)
^(?!.*?(?:0(?:12|98)|123|234|3(?:45|21)|4(?:56|32)|5(?:67|43)|6(?:78|54)|7(?:89|65)|876|987))(?!.*?(.)\1{2})[0-9]{4}
**要更好地查看图像,只需右键单击图像并在新窗口中选择视图
此正则表达式将执行以下操作:
要求字符串长度为4个数字
不允许相同的三个连续数字。例如。 111
不行。
678
或321
不行。现场演示
https://regex101.com/r/qS3bO8/2
示例文字
So for example all of these numbers are what I would need to not pass validation with the regex:
1234
6543
4567
3333
3331
1333
1239
3219
1789
2543
While numbers like these would pass:
0443
6690
0420
6798
样本匹配
MATCH 1
0. [186-190] `0443`
MATCH 2
0. [191-195] `6690`
MATCH 3
0. [196-200] `0420`
MATCH 4
0. [201-205] `6798`
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?! 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:
----------------------------------------------------------------------
12 '12'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
98 '98'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
123 '123'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
234 '234'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
3 '3'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
45 '45'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
21 '21'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
4 '4'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
56 '56'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
32 '32'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
5 '5'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
67 '67'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
43 '43'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
6 '6'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
78 '78'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
54 '54'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
7 '7'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
89 '89'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
65 '65'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
876 '876'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
987 '987'
----------------------------------------------------------------------
) 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 and capture to \1:
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\1{2} what was matched by capture \1 (2 times)
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
----------------------------------------------------------------------