Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 re模块过滤Linux命令输出?_Python - Fatal编程技术网

如何使用python re模块过滤Linux命令输出?

如何使用python re模块过滤Linux命令输出?,python,Python,我已经编写了以下用于检测RAID类型的程序。当我运行以下脚本时,re模块与字符串不匹配,并且总是给我Raid Type not found输出 import subprocess import re RAIDTYPE = subprocess.Popen('lspci -mm |grep -i "RAID bus controller"', shell=True, stdout=subprocess.PIPE) output=RAIDTYPE.communicate()[0] matchO

我已经编写了以下用于检测RAID类型的程序。当我运行以下脚本时,
re
模块与字符串不匹配,并且总是给我
Raid Type not found
输出

import subprocess
import re

RAIDTYPE = subprocess.Popen('lspci -mm |grep -i "RAID bus controller"', shell=True, stdout=subprocess.PIPE)

output=RAIDTYPE.communicate()[0]

matchObj = re.match(r'(.*) MegaRAID (.*?) .*', output, re.M|re.I)
matchObj1 = re.match(r'(.*) 3ware (.*?) .*', output, re.M|re.I)

if matchObj:
    print("This is MegaRAID")
elif matchObj1:
    print("This is 3Ware Raid")
else:
    print("Raid Type not found")

似乎正则表达式不正确。这应该起作用:


re.match(r'.*MegaRAID.*',output,re.M | re.I)

您能否打印输出并检查子流程的结果。Popen您确定正则表达式正确吗?我会做一些类似于re.match(r.*MegaRAID.*',output,re.M | re.I)@arno\u v谢谢,它正在工作