3

I have tried

re.findall(r'(\d\*\*\d)','3*2**3**2*5**4**')

The output is ['2**3', '5**4']. My desired output is ['2**3','3**2', '5**4']. What change is needed in re?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sandepp
  • 532
  • 2
  • 6
  • 16

1 Answers1

6

Change your regex to use a lookahead assertion which will not consume the string while matching:

import re

string = '3*2**3**2*5**4**'
print(re.findall(r'(?=(\d\*\*\d))', string))
>> ['2**3', '3**2', '5**4']
DeepSpace
  • 78,697
  • 11
  • 109
  • 154