将 Popen.communicate() 输出与正则表达式匹配不起作用
我的代码大致如下(整个代码有点太长,无法在此处复制):
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() 标准输出,但这也没有帮助(此外, match() 不应该使这变得不必要吗?)。
当我使用 repr() 打印标准输出时,字符串看起来像这样
'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00\n'
,如果我尝试对此进行 match() ,它会不匹配。这是制表符和换行符的问题吗?如果是,我该怎么办?
I have code that roughly looks like this (the entire code is a bit too long to copy here):
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() doesn't match this, but if I copy the stdout from the print statement and use that string in the above script as the value for stdout, it matches. So the regexp pattern should be all right. Also, if I read the string from stdin (stdout = sys.input.read()) it again works.
I've tried to rstrip() stdout as well, but that didn't help either (besides, shouldn't match() make this unnecessary?).
When I print stdout with repr() the string looks like
'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00\n'
and if I try to match() to this it doesn't match. Is this an issue with the tab and newline characters and if so, what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的正则表达式中似乎仍然存在拼写错误或导致其不匹配的错误(无关的
}
,空格太多)。尝试
看看是否有帮助。
另外,尝试一下
re.search()
与re.match()
,看看是否有任何区别。There still seem to be either typos in your regex or errors that lead to it not matching (extraneous
}
, too much whitespace).Try
and see if that helps.
Also, try
re.search()
vs.re.match()
and see if that makes any difference.您确定 stdout 中没有前导空格或此类不可见字符吗?如果您复制粘贴它们后面的内容而不是这些字符,它将解释为什么您的“手动”测试有效。
如果是这样,您可能想要执行
re.search
(在任何地方匹配)而不是re.match
(在开头匹配)或删除这些前导字符。Are you sure there is no leading space or such invisible characters in stdout ? If you copy paste what follow them but not these characters it would explain why your test 'by hand' works.
If so maybe you want to perform a
re.search
(match anywhere) instead ofre.match
(match at beginning) or remove these leading characters.您的正则表达式有一些随机字符,正确版本的所有字符都匹配:
Your regex has some random characters, with correct version of it everything matches: