正则表达式从字符串中删除语句

时间:2018-05-23 20:02:38

标签: python regex string python-3.x

我对正则表达式很新,并且在尝试从一串文本中删除2个字符串时遇到了一些问题。

我使用python3并尝试过以下操作。

'\W*(Command|exited|with|status|0)\W*

匹配字符串:Command exited with status 0

我也在寻找匹配以下内容的正则表达式。

=== stdout ===

我试过了:

'\W*(===|stdout|===)\W*

对此的任何帮助将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

使用' string = re.sub(' === stdout ===','',string)'无所谓地提交您想要的文字('')。以下是一些示例代码:

import re

string = 'i think New Roman"=== stdout ===>but I don\'t he=Command exited with status 0"Arial">fun stuff'

string = re.sub('Command exited with status 0', '', string)
string = re.sub('=== stdout ===', '', string)

print(string)

请务必使用' \ |'在您的字符串中而不仅仅是' |'