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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM