1

I have text like:

"ababbabbba"

I want to extract the characters as a list between a. For the above text, I am expecting output like:

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

I have used:

re.split(r'a(.*?)a', data)[1:-1]

But it doesn't work.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    does [this](https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches) answer your question? – timgeb Dec 07 '21 at 17:03
  • 2
    Does this answer your question? [Python regex find all overlapping matches?](https://stackoverflow.com/questions/5616822/python-regex-find-all-overlapping-matches) – Wais Kamal Dec 07 '21 at 17:06
  • Why not something like: ['a' + t + 'a' for t in data.split('a')][1:-1] – Eric Marchand Dec 07 '21 at 17:07

1 Answers1

2

If you are willing to use findall instead of split this works.

import re

s = "ababbabbba"

print(re.findall(r'(?=(a[^a]+a))',s))

prints:

['aba', 'abba', 'abbba']
kpie
  • 9,588
  • 5
  • 28
  • 50