简体   繁体   中英

how to match a regular expression like (%i1) in python pexpect

I want to use maxima from python using pexpect, whenever maxima starts it will print a bunch of stuff of this form:

$ 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)

i would like to start up pexpect like so:

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 

how do i match the starting banner up to the prompt (%i1)? and so on, also maxima increments the (%i1)'s by one as the session goes along,

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

so the next expect would be:

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

how do i match the (incrementing) integers? Basically i need to match the (%i#)'s while printing the (%o#)'s.

This regex matches it: \\(%i\\d\\) . If you needed to match the (%o#) s just replace i with o in the needle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM