Expect - 如何在一个交互循环中执行多个正则表达式匹配?
我试图在一个交互会话期间在同一屏幕输出上执行多个正则表达式匹配。使用以下代码,我收到错误消息:“不能多次使用 -o”
最终,我想使用几个正则表达式从每个输出屏幕中提取几个小数据变量,如这个问题。我想要做的事情是否可能,如果可以的话,正确的语法是什么?
interact {
#...
#... actions during interact loop to perform with variables extracted
#...
#variable extraction from output ------------------------------------
-o -nobuffer -re {(\[1;14H[a-zA-Z0-9]{1})[0-9]{5}} {
#get po number
set poraw $interact_out(0,string)
#get just po out
set po [string range $poraw 6 11]
#switch to lowercase
set po [string tolower $po]
#send_user " stored po: $po"
}
#get cost from po detail
#ex. 001b[14;27H 20.1900
-o -nobuffer -re {(\[14\;27H)[0-9]{0-6}\.{1}[0-9]{4}} {
set pocost $interact_out(0,string)
send_user " stored po cost: $pocost"
}
}
编辑: 所以有效的代码如下所示:
interact {
#...
-o
-nobuffer -re {(\[1;14H[a-zA-Z0-9]{1})[0-9]{5}} {
#get po number
set poraw $interact_out(0,string)
#get just po out
set po [string range $poraw 6 11]
#switch to lowercase
set po [string tolower $po]
}
#get cost from po detail
#ex. 001b[14;27H 20.1900
-nobuffer -re {(\[14\;27H) *[0-9]{0,6}\.{1}[0-9]{4}} {
set pocostraw $interact_out(0,string)
set pocosttrim [string range $pocostraw 7 17]
set pocost [string trimleft $pocosttrim ]
send_user " stored po cost: $pocost"
}
}
I'm trying to perform multiple regular expression matches on the same screen output, during one interact session. With the following code I'm getting the error message: "cannot use -o more than once"
Ultimately I want to extract several small data variables from each screen of output, using several regular expressions as detailed in this question. Is what I'm trying to do possible, if so what is the correct syntax?
interact {
#...
#... actions during interact loop to perform with variables extracted
#...
#variable extraction from output ------------------------------------
-o -nobuffer -re {(\[1;14H[a-zA-Z0-9]{1})[0-9]{5}} {
#get po number
set poraw $interact_out(0,string)
#get just po out
set po [string range $poraw 6 11]
#switch to lowercase
set po [string tolower $po]
#send_user " stored po: $po"
}
#get cost from po detail
#ex. 001b[14;27H 20.1900
-o -nobuffer -re {(\[14\;27H)[0-9]{0-6}\.{1}[0-9]{4}} {
set pocost $interact_out(0,string)
send_user " stored po cost: $pocost"
}
}
EDIT:
So the code that worked looks like this:
interact {
#...
-o
-nobuffer -re {(\[1;14H[a-zA-Z0-9]{1})[0-9]{5}} {
#get po number
set poraw $interact_out(0,string)
#get just po out
set po [string range $poraw 6 11]
#switch to lowercase
set po [string tolower $po]
}
#get cost from po detail
#ex. 001b[14;27H 20.1900
-nobuffer -re {(\[14\;27H) *[0-9]{0,6}\.{1}[0-9]{4}} {
set pocostraw $interact_out(0,string)
set pocosttrim [string range $pocostraw 7 17]
set pocost [string trimleft $pocosttrim ]
send_user " stored po cost: $pocost"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
manexpect
,关于交互
的部分:所以看起来
-o
改变了从它出现的地方到interact
块末尾的行为。因此,它只能出现一次是有道理的。现在解决方案很明显:将所有输出表达式放在
interact
末尾,并在第一个表达式之前添加-o
。From
man expect
, section aboutinteract
:So it seems that the
-o
changes the behavior from where it appears till the end of theinteract
block. Thus, it makes kind of sense that it can appear only once.The solution is now obvious: put all the output expressions together at the end of the
interact
and add the-o
just before the first one.