gpt4 book ai didi

python - 将 Popen.communicate() 输出与正则表达式匹配不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 19:35:13 24 4
gpt4 key购买 nike

我的代码大致如下(整个代码有点太长,无法复制到此处):

import re
from subprocess import Popen, PIPE

goodOutput = re.compile(r'\S+: 0x[0-9a-fA-F]{8} \d \d\s+->\s+0x[0-9a-fA-F]{8}')

p = Popen(['/tmp/myexe', param], stdout=PIPE, stderr=PIPE, cwd='/tmp')

stdout, stderr = p.communicate()

ret = goodOutput.match(stdout)
if ret == None:
print "No match in: " + stdout

match() 与此不匹配,但如果我从 print 语句复制 stdout 并使用上面脚本中的该字符串作为 stdout 的值,它就会匹配。所以正则表达式模式应该没问题。另外,如果我从 stdin (stdout = sys.input.read()) 读取字符串,它会再次工作。

我也尝试过 rstrip() stdout,但这也没有帮助(此外,match() 不应该使这变得不必要吗?)。

当我用 repr() 打印标准输出时,字符串看起来像

'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00\n'

如果我尝试使用 match() 来匹配它,它会不匹配。这是制表符和换行符的问题吗?如果是,我该怎么办?

最佳答案

您的正则表达式中似乎仍然存在拼写错误或导致其不匹配的错误(无关的 },空格过多)。

尝试

goodOutput = re.compile(r"\s*[^:]:s*0x[0-9a-fA-F]{8}\s+\d\s+\d\s+->\s+0x[0-9a-fA-F]{8}"`

看看是否有帮助。

另外,尝试一下 re.search()re.match() ,看看是否有任何区别。

关于python - 将 Popen.communicate() 输出与正则表达式匹配不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3921106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com