Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式从字符串中删除statment_Python_Regex_String_Python 3.x - Fatal编程技术网

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

Python 正则表达式从字符串中删除statment,python,regex,string,python-3.x,Python,Regex,String,Python 3.x,我对regex非常陌生,在尝试从文本字符串中删除2个字符串时遇到了一些问题 我正在使用python3,并尝试了以下方法 '\W*(Command|exited|with|status|0)\W* === stdout === 要匹配字符串:命令已退出,状态为0 我也在寻找正则表达式来匹配以下内容 '\W*(Command|exited|with|status|0)\W* === stdout === 我试过: '\W*(===|stdout|===)\W* 在此方面的任何帮助都将不胜感

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

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

'\W*(Command|exited|with|status|0)\W*
=== stdout ===
要匹配字符串:命令已退出,状态为0

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

'\W*(Command|exited|with|status|0)\W*
=== stdout ===
我试过:

'\W*(===|stdout|===)\W*
在此方面的任何帮助都将不胜感激。
谢谢

使用'string=re.sub'===stdout===',string'来将所需的文本进行无意义的sub。下面是一些示例代码:

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)

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

Command | exited | with | status | 0:|是否为单词分隔符。这里甚至不需要正则表达式。只需使用您的字符串即可。替换===stdout==,,对于另一个示例,同样的事情是单引号是打字错误吗?否则,它将匹配该字符串中的单词。只从模式的第一个元素开始创建/调试正则表达式,并且只从与预期匹配的元素开始,然后添加模式的下一个元素。由于您将“with”或“command”的任何实例替换为,而不是请求的字符串“command exit with status”我稍微修改了代码,以便更好地匹配请求的字符串。我没有意识到使用了“|”来代替“空格”。谢谢大家,让我试一下这一秒。好吧,这样就行了,但是我又忘记了一个,特别是没有stderr,当我尝试重新使用sub'noster'时,字符串起作用了,但是,它离开了,只是删除了noster。