If I have a sentence similar to the following:
aabcadeaaabbacababe
and I want to get all of the text where it starts with c
and ends with e
, so that it matches like:
['ad', 'abab']
is it possible to achieve this? I have tried using lookarounds and c(.*)e
, but instead of returning as separate entities in a list, they are combined into one substring that only looks for the first c
and the last e
.
Try to change your c(.*)e
to c(.*?)e
, which will make the match non-greedy.
import re
r = re.findall(r'c(.*?)e', 'aabcadeaaabbacababe')
print(r) # ['ad', 'abab']