The question of getting an in-between string has been asked before but does not cover certain conditions. In my case, I may have a string like the following:
subject = '"lorem ipsum", "foo", "baz", "bar", "lorem ipsum", "bar", "ipsum", "foo", "baz", "bar"'
I want to extract all instances of text between foo", " and ", "bar". The traditional "Get between" answer is:
import re
result = re.findall('foo", "(.*)", "bar', subject)
print(result)
which returns only 1 result string:
'baz", "bar", "lorem ipsum", "bar", "ipsum", "foo", "baz'
but what I want to return is a list of all possible "Between" strings like:
[
'baz',
'baz", "bar", "lorem ipsum',
'baz", "bar", "lorem ipsum", "bar", "ipsum", "foo", "baz',
'baz'
]
So given two substrings (Start & end) and a subject string, how can I get all possible substrings between start and end in subject? The most computationally efficient solution is of course desirable.