我有几个数字字符串,如下所示:
0000000
0000011
0000012
我想验证模式是这样的:
AAAAABC
其中A
,B
和C
都是不同的数字。因此,在示例中,只应匹配0000012
。
到目前为止我的正则表达式是(\d)\1\1\1\1\d\d
,但它不能确保数字不同。我需要做什么?
答案 0 :(得分:3)
我想你想要
(\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上查看。