0

I need to find from "aaaa" -> 'aa', 'aa', 'aa', 'aaa', 'aaa', 'aaaa'.

I tried re.findall(r'(.)\1{1,}'), but all I find is 'a'.

From this question, I attempted to form a regex to get the desired results. However, there are format braces within the regex count specifier braces.

I think I've seen how that is handled but can't find it.

for n in range(1, 3):
    for m in re.finditer(r'(?=((.)\2{{0}}))'.format(n), 'aaaa'):
        print(m.group(1))

This gives:

a
a
a
a
a
a
a
a

but I want:

aa
aa
aa
aaa
aaa
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Chris Charley
  • 6,403
  • 2
  • 24
  • 26
  • 1
    Please post the answer as an answer rather than editing it into your question. That way the site will know your question has been answered and people can vote on the solution. – John Kugelman Dec 21 '22 at 02:56

1 Answers1

0

I found the answer here. Just had to double up the braces for the regex count specifier.

>>> for n in range(1, 3):
    for m in re.finditer(rf'(?=((.)\2{{{n}}}))', 'aaaa'):
        print(m.group(1))

        
aa
aa
aa
aaa
aaa
>>> 
Chris Charley
  • 6,403
  • 2
  • 24
  • 26