2

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

0000000
0000011
0000012

我想验证模式是这样的:

AAAAABC

其中A和都是不同的数字BC所以在例子中, only0000012应该匹配。

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

4

1 回答 1

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

于 2013-04-05T12:37:04.903 回答