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

2024-10-16 20:53:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对regex非常陌生,在尝试从文本字符串中删除2个字符串时遇到了一些问题。你知道吗

我正在使用python3,并尝试了以下方法。你知道吗

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

匹配字符串:Command exited with status 0

我也在寻找正则表达式匹配以下。你知道吗

=== stdout ===

我试过:

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

任何帮助都将不胜感激。 谢谢!你知道吗


Tags: 方法字符串文本statuswithstdoutcommandpython3
2条回答

使用'string=回复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)

记住在字符串中使用“\”而不是仅仅使用“\”

使用'string=回复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)

记住在字符串中使用“\”而不是仅仅使用“\”

相关问题 更多 >