expect正则捕获返回结果

expect正则捕获返回结果

expect:

expect -re "([0-9]*)([a-zA-Z]*)"
send_user "num is $expect_out(1,string), string is $expect_out(1,string)"
这里[0-9]*表示一个或多个数字,[a-zA-Z]*表示多个字母。()用于分组,它们分别存放在$expect_out(1,string)和$expect_out(2,string)中。

pexpect:python中的expect

child.expect("([0-9]*)([a-zA-Z]*)
print "num is %s, string is %s" % (child.match.group(1),child.match.group(2))
注意,pexpect 匹配字符串是从sendline的命令开始算的, 而不是命令返回结果开始。

比如,我现在执行pgrep ssh0,它的返回是ssh0的pid,如果我的expect re用"(\d+)",最后output匹配的结果是0, 其中child.before = ‘pgrep ssh’,child.after = '0'

child.sendline (“pgrep shh0”)
child.expect("(\d+)")
output = child.match.group(1)
当换成child.expect("\r\n(\d+)\r\n")后就能正确的匹配到pid了。

猜你喜欢

转载自www.cnblogs.com/lgj8/p/12296428.html
今日推荐