如何在python pexpect中匹配像(%i1)这样的正则表达式

时间:2012-12-15 10:59:50

标签: python regex pexpect

我想使用pexpect从python中使用maxima,每当maxima启动它会打印出一堆这种形式的东西:

$ maxima
Maxima 5.27.0 http://maxima.sourceforge.net
using Lisp SBCL 1.0.57-1.fc17
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1)

我想像这样启动pexpect:

import pexpect 
cmd = 'maxima'
child = pexpect.spawn(cmd)
child.expect (' match all that stuff up to and including (%i1)')
child.sendline ('integrate(sin(x),x)')
chil.expect( match (%i2) i think ; see below sample session  ) 
print child.before 

如何匹配起始横幅到提示符(%i1)? 依此类推,当会话进行时,最大值也会将(%i1)的值递增1,

(%i1) integrate(sin(x),x);
(%o1)                              - cos(x)
(%i2) integrate(log(x),x);
(%o2)                            x log(x) - x
(%i3)

所以接下来的期望是:

child.expect ('match (%i2)')
child.sendline ('integrate(log(x),x)')
child.expect( match (%i3) ) 
print child.before 

我如何匹配(递增)整数? 基本上我需要在打印(%o#)时匹配(%i#)。

1 个答案:

答案 0 :(得分:1)

此正则表达式匹配它:\(%i\d\)。如果您需要匹配(%o#),只需将i替换为针中的o