如何在TCL/EXPECT中使用$ expect_out分配变量? [英] How can I assign a variable using $expect_out in TCL/EXPECT?
问题描述
如果我想使用以下正则表达式匹配DEF_23
:
If I want to match DEF_23
using the following regexp:
expect {
-re "DEF_\[0-9]*"
set result $expect_out(1,string)
}
为什么说no such element in array
?
$expect_out
如何工作,如何使用正则表达式捕获DEF
并将其分配给变量result
?
why does it say no such element in array
?
How does $expect_out
work, and how can I capture the DEF
using a regexp and assign it to the variable result
?
推荐答案
您正在寻找expect_out(0,string)
-如果在正则表达式中捕获了括号,则会填充数组元素1,string
.
You're looking for expect_out(0,string)
-- the array element 1,string
would be populated if you had capturing parentheses in your regular expression.
期望联机帮助页记录了 期望命令的文档中的Expect_out :
The expect manpage documents the use of expect_out in the documentation of the expect command:
匹配模式(或eof或full_buffer)后,所有匹配的和以前不匹配的输出将保存在变量 expect_out(buffer)中.最多9个正则表达式子字符串匹配项通过 expect_out(9,string)保存在变量 expect_out(1,string)中.如果在模式之前使用 -indices 标志,则10个字符串的开始和结束索引(以适合 lrange 的形式)存储在变量中Expect_out(X,start)和 expect_out(X,end)其中X是一个数字,对应于缓冲区中子字符串的位置. 0表示与整个模式匹配的字符串,并且为glob模式和regexp模式生成.
Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable expect_out(buffer). Up to 9 regexp substring matches are saved in the variables expect_out(1,string) through expect_out(9,string). If the -indices flag is used before a pattern, the starting and ending indices (in a form suitable for lrange) of the 10 strings are stored in the variables expect_out(X,start) and expect_out(X,end) where X is a digit, corresponds to the substring position in the buffer. 0 refers to strings which matched the entire pattern and is generated for glob patterns as well as regexp patterns.
联机帮助页中有一个说明性示例.
There is an illustrative example in the manpage.
这篇关于如何在TCL/EXPECT中使用$ expect_out分配变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!