I am looking to find all strings between two substrings while keeping the first substring and discarding the second. The substrings might be one of several values though. For example, if these are the possible substrings:
subs = ['MIKE','WILL','TOM','DAVID']
I am looking to get the string between any of these like this:
Input:
text = 'MIKE an entry for mike WILL and here is wills text DAVID and this belongs to david'
Output:
[('MIKE': 'an entry for mike'),
('WILL': 'and here is wills text'),
('DAVID': 'and this belongs to david')]
Trailing spaces are not important. I have tried:
re.findall('(MIKE|WILL|TOM|DAVID)(.*?)(MIKE|WILL|TOM|DAVID)',text)
which only returns the first occurrence and retains the end substring. Not too sure of the best approach.