I have next code:
test.py:
import pexpect
import sys
p = pexpect.spawn("ping 10.192.225.199", encoding="utf-8")
while True:
try:
index = p.expect([".+", pexpect.EOF, pexpect.TIMEOUT], timeout=1)
if index == 0:
print("===")
print(p.after)
print("===")
except Exception as e:
print(e)
Execution:
$ python3 test.py
===
PING 10.192.225.199 (10.192.225.199) 56(84) bytes of data.
===
===
64 bytes from 10.192.225.199: icmp_seq=1 ttl=63 time=0.607 ms
===
===
64 bytes from 10.192.225.199: icmp_seq=2 ttl=63 time=0.587 ms
===
...
It looks .+
could fetch the whole line of ping command output
during one time iteration.
But, someone suggest this to me, in the official document, it said:
Beware of + and * at the end of patterns
Remember that any time you try to match a pattern that needs look-ahead that you will always get a minimal match (non greedy).
For example, the following will always return just one character:
child.expect ('.+')
This example will match successfully, but will always return no characters:
child.expect ('.*')
What does will always return just one character
mean? Why I can get a full line in my minimal example? Isn't it should be one character per loop?
BTW, it's extremely strange that if I change .+
to .*
, then just as the document said: will always return no characters
. The behavior for .*
same as the document said, but .+
not ...
from What does pexpect pattern ".+" do?
No comments:
Post a Comment