python - 正则表达式从字符串中删除语句
问题描述
我对正则表达式很陌生,并且在尝试从文本字符串中删除 2 个字符串时遇到了一些问题。
我正在使用 python3 并尝试了以下方法。
'\W*(Command|exited|with|status|0)\W*
要匹配字符串:Command exited with status 0
我也在寻找匹配以下内容的正则表达式。
=== stdout ===
我努力了:
'\W*(===|stdout|===)\W*
对此的任何帮助将不胜感激。谢谢!
解决方案
使用 '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)
记得使用'\|' 在您的字符串中,而不仅仅是 '|'
推荐阅读
- python - 通过 multiprocessing.pool 的 Python3 并行代码比顺序代码慢
- vba - 打开文件中的宏如果你在开会,点击“确定”,如果没有,在excel中再次提及
- logistic-regression - 如何使用主成分分析进行逻辑回归
- android - 如何从 firebase 数据库中洗牌并显示有限的值?
- java - 在java中查找阿姆斯特朗数
- ocaml - OCaml 中的输出关键字
- laravel - 从 Lambda 即时推送消息到 Web 主机?
- excel - 将 NESTED IF 语句转换为 vba 代码
- c# - 使用 Botframework V3 将数据从 Web 表单发送到 Web 聊天机器人
- amazon-dynamodb - AppSync BatchGet 过滤返回限制错误