我有几个数字字符串,如下所示:
0000000
0000011
0000012
我想验证模式是这样的:
AAAAABC
其中A
和都是不同的数字B
。C
所以在例子中, only0000012
应该匹配。
到目前为止,我的正则表达式是(\d)\1\1\1\1\d\d
,但它不能确保数字不同。我需要做什么?
我想你想要
(\d)\1{4}(?!\1)(\d)(?!\1|\2)\d
解释:
(\d) # Match a digit, capture in group 1
\1{4} # Match the same digit as before four times
(?!\1) # Assert that the next character is not the same digit as before
(\d) # Match another digit, capture in group 2
(?!\1|\2) # Assert the next character is different from both previous digits
\d # Match another digit.
在regex101上查看。