Reputation: 661
I'm trying to create a regular expression that will tell me if I have two or more repeating numbers in a string separated by a comma. For example "10,2,3,4,5,6,7,8,9,10" would return true because there are two tens.
I think I am close. So far I have:
if re.match(r"(\d+),.+\1",line):
Thanks!
Upvotes: 0
Views: 100
Reputation: 147216
You don't need regex for this. Just convert into a list using split
, then convert that into a set (which will contain only the unique numbers in the list) and compare the lengths:
line = "10,2,3,4,5,6,7,8,9,10"
lst = line.split(',')
unq = set(lst)
if (len(lst) != len(unq)):
# non-unique numbers present
If you want to use regex, you need to use re.search
rather than re.match
, as re.match
requires matches to begin at the start of the string, which would preclude matching 2
in "1,2,3,4,5,6,2,7,8,9,10"
. Also, you need to surround your (\d+)
with word breaks (\b
), so that 1
in "1,2,3,4,5,6,2,7,8,9,10"
doesn't then match against the 1
in 10
. This regex will give you the results you want:
m = re.search(r'\b(\d+)\b.*\b\1\b', line)
if m:
print('Duplicate number ' + m.group(1))
Upvotes: 5