The regular expression in Python for digits that are repeated but not necessarily in sequence is:
(\d).*\1
This expression matches any digit that appears at least twice in a string.
(\d)
captures a digit .*
matches any number of characters (including digits) \1
matches the same digit captured by the first groupExample:
import re
string = "1234567891011"
pattern = re.compile(r'(\d).*\1')
matches = pattern.findall(string)
print(matches) # Output: ['1']
In the above example, the pattern matches the digit '1'
because it appears twice in the string: once as the first digit and again as part of the number '11'
.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-07-03 11:00:00 +0000
Seen: 9 times
Last updated: May 11 '21
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?