首页 > 解决方案 > 正则表达式从字符串中删除语句

问题描述

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

我正在使用 python3 并尝试了以下方法。

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

要匹配字符串:Command exited with status 0

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

=== stdout ===

我努力了:

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

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

标签: pythonregexstringpython-3.x

解决方案


使用 '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)

记得使用'\|' 在您的字符串中,而不仅仅是 '|'


推荐阅读