I would like to find all alternating digits in a string using regular expressions. An alternating digit is defined as two equal digits having a digit in between; for example, 1212 contains 2 alternations (121 and 212) and 1111 contains 2 alternations as well (111 and 111). I have the following regular expression code:
s = "1212"
re.findall(r'(\d)(?:\d)(\1)+', s)
This works for strings like "121656", but not "1212". This is a problem to do with overlapping matches I think. How can I deal with that?