(d)1+
Explanation:
# match word boundary
(d) # match digit remember it
1+ # match one or more instances of the previously matched digit
# match word boundary
If 1
should also be a valid match (zero repetitions), use a *
instead of the +
.
If you also want to allow longer repeats (123123123
) use
(d+)1+
If the regex should be applied to the entire string (as opposed to finding "repeat-numbers in a longer string), use start- and end-of-line anchors instead of
:
^(d)1+$
Edit: How to match the exact opposite, i. e. a number where not all digits are the same (except if the entire number is simply a digit):
^(d)(?!1+$)d*$
^ # Start of string
(d) # Match a digit
(?! # Assert that the following doesn't match:
1+ # one or more repetitions of the previously matched digit
$ # until the end of the string
) # End of lookahead assertion
d* # Match zero or more digits
$ # until the end of the string
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…