首页 > 其他 > 详细

expect正则捕获返回结果

时间:2020-02-11 20:00:12      阅读:119      评论:0      收藏:0      [点我收藏+]

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了。

expect正则捕获返回结果

原文:https://www.cnblogs.com/lgj8/p/12296428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!