Regex to find repeating numbers

\b(\d)\1+\b

Explanation:

\b   # match word boundary
(\d) # match digit remember it
\1+  # match one or more instances of the previously matched digit
\b   # 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

\b(\d+)\1+\b

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 \b:

^(\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

Leave a Comment