How to write Python Regular Expression find repeating digits in a number?



In this article you will find to write regular expression in Python to find repeating digits in the given number. Regular expression is a special feature that helps you find matching parts of a string.

It can find all repeated characters or digits in a number. Finding repeating digits can help in many areas like data analysis or validation.

For example, check if a number like 1223 has repeated digits "2" or "3". So in this example 2 is the repeated digit. Let us see how you can perform this task practically using Python Regex.

Regex Pattern for Repeated Digits

A repeating digit means the same number appears at least twice in a row like "22" in "122345". So here is the regex pattern for finding repeated digits -

r'(\d)\1+'

In the above pattern the '\d' is used to capture a single digit and '\1+' is used to check that the same digit appears again. There are several ways to find repeated digits in a number using Regex like -

Example: Find First Repeating Digit

The following example will use the re.search() method to find the first repeating digit in the given number. So our program basically tries to convert n to a string. Then it will search for the first repeating digit. In the end it will return the matched value or "No repeats."

# Import re module
import re

def find_repeat(n):
    m = re.search(r'(\d)\1+', str(n))
    if m:
        return m.group()
    else:
        return "No repeats"

print("Output:", find_repeat(122345))  

Output

This will create the below outcome -

22

Example: List All Repeating Digits

This is another program to list all the repeating digits instead of just the first match in the given number. Here we will use findall() to get all the matches. Then we will return a list of repeating digits.

# Import re module
import re

def find_all_repeats(n):
    # Captures full repeated group and digit
    m = re.findall(r'((\d)\2+)', str(n))  
    if m:
        # Return only the full repeated parts
        return [group[0] for group in m]  
    else:
        return ["No repeats"]

print("Output:", find_all_repeats(122334455))

Output

This will generate the below result -

['22', '33', '44', '55']

Example: Count Repeating Digits

This program shows another functionality to count how many times a repeating digit appears in the given number.Here we are using a dictionary to store the count. And we will check how each repeating digit appears.

For example if the number is 12233445555 so in this number 22 is appearing 1 time, 33 is 1 time, 44 is 1, and 55 is 2 times.

# Import re module
import re

def count_repeats(n):
    m = re.findall(r'((\d)\2+)', str(n))
    counts = {}
    for full, digit in m:
        counts[full] = counts.get(full, 0) + 1
    return counts

print("Output:", count_repeats(12233445555))

Output

This will produce the following result -

Output: {'22': 1, '33': 1, '44': 1, '5555': 1}

Example: Find Longest Repeating Sequence

The program finds the longest sequence of repeated digits in a given number.

It shows the digit that appears the most frequently in a row. For example if the number is 122333444455555 here the longest repeating group is "55555".

Here we use findall() to get all the repeated groups, then use max() to find the longest one. If no repeats are found, it will return "No repeats".

# Import re module
import re

def longest_repeat(n):
    m = re.findall(r'((\d)\2+)', str(n))
    if m:
        return max([grp[0] for grp in m], key=len)
    else:
        return "No repeats"

print("Output:", longest_repeat(122333444455555))

Output

This will lead to the following outcome -

Output: 55555
Updated on: 2025-06-06T14:58:23+05:30

747 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements