Home > Blockchain >  How to extract the text between the word using RegEx?
How to extract the text between the word using RegEx?

Time:12-08

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.

CodePudding user response:

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']
  • Related