I am trying to fetch the last occurrence of the patterns where the string contains similar patterns in between. E.g: my string is : "abc abc abc efg 123 abc 123 abc abc xyz 123" I want to capture the pattern between abc and 123. My desired output is: ['abc efg 123', 'abc 123', 'abc xyz 123']
So I used regex 'abc.*?123'. But this is giving as follows: ['abc abc abc efg 123', 'abc 123', 'abc abc xyz 123']
I don't want first occurrence of first pattern to second pattern, I need last occurrence of first pattern to second pattern
import re
a="abc abc abc efg 123 abc 123 abc abc xyz 123"
print a
b=re.findall(r'abc.*?123',a)
print "Output is: "+str(b)
Output is: ['abc abc abc efg 123', 'abc 123', 'abc abc xyz 123']
I expect the output as: ['abc efg 123', 'abc 123', 'abc xyz 123']