kbyrd
kbyrd

Reputation: 3351

How do I get pexpect to match everything BUT a given line?

Let's say there's a call to pexpect.expect like this:

ret = pex.expect([re.escape(line), pexpect.EOF, pexpect.TIMEOUT], timeout)
if ret == 0:
    do_stuff()
elif ret == 1:
    do_eof_stuff()
elif ret == 2:
    do_timeout_stuff()

Instead of matching the line I'm interested in, I want to switch things around and match anything BUT that line, in addition to handling EOF and TIMEOUT. Sort of like this:

ret = pex.expect([not re.escape(line), pexpect.EOF, pexpect.TIMEOUT], timeout)
if ret == 0:
    do_error_stuff()
elif ret == 1:
    do_eof_stuff()
elif ret == 2:
    do_timeout_stuff()
else:
   # Our line "matched" so do stuff
   do_stuff()

I know 'not re.escape(line)' isn't valid, it's just a way to demonstrate what I want. What would I actually use in it's place?

Upvotes: 1

Views: 3650

Answers (1)

cwgem
cwgem

Reputation: 2809

I toyed around with this using a negative lookahead:

ret = pex.expect([r'^((?!' + re.escape(line) + ').)*$', pexpect.EOF, pexpect.TIMEOUT], timeout)

The caveat here is that if line was hello world for example:

hello world -> won't match
hello world 2 -> won't match
my hello world -> won't match
hello worl -> will match

Not sure how specific you need that.

Upvotes: 1

Related Questions