首页 > 解决方案 > 删除特定的字符集?

问题描述

我知道这样的代码

translated1 = str(''.join( c for c in translated2 if c not in "[']" ))

将删除 [ 或 ' 或 ] 的任何实例,但我将如何对其进行编码以便它完全删除“---”。

我不希望它删除任何“-”实例,前提是这三个同时在一起。

我怎样才能做到这一点?谢谢!

标签: pythoncharacter

解决方案


这可以通过正则表达式很容易地完成。您可以在此处阅读有关 python 正则表达式的更多信息。

你可以这样使用它-

import re
str1 = 'there is three --- and now single - and now two --'
str2 = re.sub('---', '', str1)
print(str2)

输出-

there is three  and now single - and now two --

演示


推荐阅读