如何确保数字中的某些数字不相同

时间:2013-04-05 12:33:54

标签: regex position

我有几个数字字符串,如下所示:

0000000
0000011
0000012

我想验证模式是这样的:

AAAAABC

其中ABC都是不同的数字。因此,在示例中,只应匹配0000012

到目前为止我的正则表达式是(\d)\1\1\1\1\d\d,但它不能确保数字不同。我需要做什么?

1 个答案:

答案 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上查看。