有没有一种有效的方法可以删除 python 中两个分隔符之间的子字符串?例如来自
"This $\textbf{word}$ must be deleted"
获得
"This must be deleted"
如果可以,我宁愿不用正则表达式包。
此外,如果分隔符不相等怎么办,例如来自
"This {word} must be deleted"
获得
"This must be deleted"
最佳答案
如果你不想使用正则表达式,你可以按照以下几行做一些事情:
s = "This $\textbf{word}$ must be deleted and this $here$ too"
d = '$'
''.join(s.split(d)[::2])
# 'This must be deleted and this too'
这会在分隔符处拆分,并且只保留所有其他标记。如果你想摆脱双空格,你可以这样做:
' '.join(x.strip() for x in s.split(d)[::2])
# 'This must be deleted and this too'
关于python - 删除python中分隔符之间的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201148/