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

发布于 2024-09-27 09:10:06 字数 759 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咆哮 2024-10-04 09:10:06

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

尝试

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() ,看看是否有任何区别。

There still seem to be either typos in your regex or errors that lead to it not matching (extraneous }, too much whitespace).

Try

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

and see if that helps.

Also, try re.search() vs. re.match() and see if that makes any difference.

書生途 2024-10-04 09:10:06

您确定 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 of re.match (match at beginning) or remove these leading characters.

逆夏时光 2024-10-04 09:10:06

您的正则表达式有一些随机字符,正确版本的所有字符都匹配:

>>> s = 'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00\n'
>>> re.match(r'\S+: 0x[0-9a-f]{8} \d \d\s+->\s+0x[0-9a-f]{8}', s, re.I).group()
'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00'

Your regex has some random characters, with correct version of it everything matches:

>>> s = 'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00\n'
>>> re.match(r'\S+: 0x[0-9a-f]{8} \d \d\s+->\s+0x[0-9a-f]{8}', s, re.I).group()
'xxx[a]: 0xff2eff00 4 7\t->\t0xff2eff00'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文