_list = ['match','regex','find']
_str = 'find the match and then find the regex later on'
根据_list,能够将_str拆分为:?
['find',' the ','match',' and then ','find',' the ','regex',' later on']
请注意,拆分字符串为其余子字符串保持空格
欢呼
答案 0 :(得分:0)
您可以将re.findall
与以下正则表达式配合使用:
import re
print([s for m in re.findall(r'(^.*?|)({0})(.*?)(?={0}|$)'.format('|'.join(_list)), _str) for s in m if s])
这将输出:
['find', ' the ', 'match', ' and then ', 'find', ' the ', 'regex', ' later on']