Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to write Python Regular Expression find repeating digits in a number?
In this article, you will learn to write regular expressions in Python to find repeating digits in a given number. Regular expressions are a powerful feature that helps you find matching patterns in strings.
Finding repeating digits is useful in many areas like data validation, pattern recognition, and number analysis. For example, in the number 1223, we can identify that "22" contains repeating digits.
Understanding the Regex Pattern
To find repeating digits, we use the pattern r'(\d)\1+':
(\d) − Captures a single digit in a group
\1+ − Matches one or more occurrences of the same digit captured in group 1
This pattern finds consecutive repeating digits like "22", "333", or "4444".
Method 1: Find First Repeating Digit
Use re.search() to find the first occurrence of repeating digits ?
import re
def find_first_repeat(number):
match = re.search(r'(\d)\1+', str(number))
if match:
return match.group()
else:
return "No repeats"
# Test the function
result = find_first_repeat(122345)
print("First repeating digits:", result)
First repeating digits: 22
Method 2: Find All Repeating Digits
Use re.findall() to get all repeating digit sequences ?
import re
def find_all_repeats(number):
matches = re.findall(r'((\d)\2+)', str(number))
if matches:
return [group[0] for group in matches]
else:
return ["No repeats"]
# Test the function
result = find_all_repeats(122334455)
print("All repeating digits:", result)
All repeating digits: ['22', '33', '44', '55']
Method 3: Count Repeating Sequences
Count how many times each repeating pattern appears ?
import re
def count_repeats(number):
matches = re.findall(r'((\d)\2+)', str(number))
counts = {}
for full_match, digit in matches:
counts[full_match] = counts.get(full_match, 0) + 1
return counts
# Test the function
result = count_repeats(12233445555)
print("Count of repeating digits:", result)
Count of repeating digits: {'22': 1, '33': 1, '44': 1, '5555': 1}
Method 4: Find Longest Repeating Sequence
Identify the longest sequence of consecutive repeating digits ?
import re
def longest_repeat(number):
matches = re.findall(r'((\d)\2+)', str(number))
if matches:
return max([group[0] for group in matches], key=len)
else:
return "No repeats"
# Test the function
result = longest_repeat(122333444455555)
print("Longest repeating sequence:", result)
Longest repeating sequence: 55555
Comparison of Methods
| Method | Function | Returns | Best For |
|---|---|---|---|
| First Repeat | re.search() |
First match only | Quick validation |
| All Repeats | re.findall() |
List of all matches | Complete analysis |
| Count Repeats |
re.findall() + dict |
Dictionary with counts | Frequency analysis |
| Longest Repeat |
re.findall() + max() |
Longest sequence | Pattern identification |
Conclusion
Regular expressions provide an efficient way to find repeating digits in numbers. Use re.search() for the first match, re.findall() for all matches, and combine with additional logic for counting or finding the longest sequence based on your specific requirements.
