如何在python pexpect中匹配类似(%i1)的正则表达式
我想使用pexpect使用python中的maxima,每当maxima启动时,它都会打印以下表单的内容:如何在python pexpect中匹配类似(%i1)的正则表达式,python,regex,pexpect,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
$ 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)匹配?
依此类推,随着会话的进行,maxima也会将(%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) 此正则表达式与之匹配:
\(%i\d\)
。如果需要匹配(%o#)
s,只需将i
替换为针中的o