1

I have a string like:

s = 'ababbabbba'

I'm trying to match all patterns matching any number of b's between a's. This is what I expect the patterns to be for s above:

['aba', 'abba', 'abbba']

This is what I've tried:

import re
re.findall('ab+a', s)

Which gives:

['aba', 'abbba']

I think that happens because any single a can only be part of a single group. Whereas my requirement would make the middle a's be part of two groups. Reading through the re documentation, I can't find any way to do this.

foxpal
  • 586
  • 4
  • 14
  • 1
    https://stackoverflow.com/q/5616822/1324033 – Sayse Feb 14 '20 at 06:41
  • 1
    Duplicate of [Python regex find all overlapping matches?](https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches) – mkrieger1 Feb 14 '20 at 09:47

1 Answers1

2

Based on the comment above, the solution is:

re.findall('(?=(ab+a))', s)
foxpal
  • 586
  • 4
  • 14