【发布时间】:2019-10-02 13:59:52
【问题描述】:
假设我有字符串:
string = "this is a test string <LW> I want to <NL>split this string<NL> by each tag I have inserted.<AB>"
我想通过在前一个函数中插入字符串中的每个自定义标签来拆分字符串:
tags = ["<LW>", "<NL>", "<AB>"]
这是所需的输出:
splitString = splitByTags(string, tags)
for s in splitString:
print(s)
输出
"this is a test string <LW>"
" I want to <NL>"
"split this string<NL>"
" by each tag I have inserted.<AB>"
所以基本上我想将字符串拆分为多个子字符串,同时将这些子字符串保留在拆分中。最快和最有效的方法是什么?我知道我可以使用 string.split 并简单地将拆分文本附加到每一行,但是我不确定如何使用多个字符串来执行此操作。
【问题讨论】:
标签: python regex string split substring