【发布时间】:2018-02-12 00:42:20
【问题描述】:
我希望找到两个子字符串之间的所有字符串,同时保留第一个子字符串并丢弃第二个。不过,子字符串可能是几个值之一。例如,如果这些是可能的子字符串:
subs = ['MIKE','WILL','TOM','DAVID']
我正在寻找这样的字符串:
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')]
尾随空格并不重要。我试过了:
re.findall('(MIKE|WILL|TOM|DAVID)(.*?)(MIKE|WILL|TOM|DAVID)',text)
仅返回第一次出现并保留结束子字符串。不太确定最好的方法。
【问题讨论】: