Expect - 如何在一个交互循环中执行多个正则表达式匹配?

发布于 2025-01-03 23:31:13 字数 1880 浏览 1 评论 0原文

我试图在一个交互会话期间在同一屏幕输出上执行多个正则表达式匹配。使用以下代码,我收到错误消息:“不能多次使用 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2025-01-10 23:31:13

来自 manexpect,关于交互的部分:

-o 标志导致任何后续键体对应用于当前进程的输出。例如,当处理在 telnet 会话期间发送不需要的字符的主机时,这可能很有用。 [强调我的]

所以看起来 -o 改变了从它出现的地方到 interact 块末尾的行为。因此,它只能出现一次是有道理的。

现在解决方案很明显:将所有输出表达式放在interact末尾,并在第一个表达式之前添加-o

From man expect, section about interact:

The -o flag causes any following key-body pairs to be applied to the output of the current process. This can be useful, for example, when dealing with hosts that send unwanted characters during a telnet session. [emphasis mine]

So it seems that the -o changes the behavior from where it appears till the end of the interact 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文