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