python - 删除python中分隔符之间的子字符串
问题描述
有没有一种有效的方法来删除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'
推荐阅读
- angular - mediaRecorder.ondataavailable 在角度 4 中不起作用
- python - 在熊猫数据框中按日期范围分组
- css - 如何在 FF 中查看用户代理特定的样式表?
- python - pandas:如何比较两列的浮点值
- tcp - TCP Socket:如何在服务器上使用 FD_ISSET() 来检测客户端的发送数据?
- css - Use SASS @each variable inside nested mixin
- c# - 如何通过在c#中传递属性来调用power shell脚本文件
- oauth-2.0 - 401 Unauthorized error when connecting to Dynamics CRM 365 using Postman
- excel - Speed up Save to PDF Excel 2016 VBA
- python - 根据字符串内容对字符串列表进行排序