-1
s = 'blah blah blah... _ABC_superman_is_cool_CBA_ ...blah blah blah...'

This is just an example, but I want to match everything between _ABC_ and _CBA_. So 'superman_is_cool'. There may be multiple sections of _ABC_..._CBA_.

re.findall('_ABC_(.*)(?=_CBA_)', s)

I tried this first, but obviously doesn't correctly work at all.

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
jairajs89
  • 4,495
  • 3
  • 18
  • 14

2 Answers2

6

I added an additional _ABC_, _CBA_ pair to make sure it finds all the matches:

>>> s = 'blah blah blah... _ABC_superman_is_cool_CBA_ ...blah blah _ABC_blah_CBA_...'
>>> re.findall('_ABC_(.*?)_CBA_', s)
['superman_is_cool', 'blah']

The ? makes the * operator non-greedy so it finds as short a match as possible. Without it the result would be ['superman_is_cool_CBA_ ...blah blah _ABC_blah'].

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Try this

re.findall('_ABC_.*_CBA_)', s)
Falmarri
  • 47,727
  • 41
  • 151
  • 191