Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex expect中的正则表达式_Regex_Tcl_Expect - Fatal编程技术网

Regex expect中的正则表达式

Regex expect中的正则表达式,regex,tcl,expect,Regex,Tcl,Expect,我对TCL/EXPECT中的以下代码有疑问: expect { timeout { puts "timeout error" } -re "Please enter the play name" { exp_send $common1 exp_continue } -re "Please enter the play name_type" { exp_send $common2

我对TCL/EXPECT中的以下代码有疑问:

expect { 
    timeout { 
        puts "timeout error"
    }
    -re "Please enter the play name" {
        exp_send $common1
        exp_continue
    }
    -re "Please enter the play name_type" {
        exp_send $common2
        exp_continue
    }
    -re "Now you can get the information of the play" {
        expect {
        -re "\r"
            }
    }
    }
如果我运行上面的代码,它将卡在第二个代码块(-re“请输入播放名称\ u类型”)。为什么会发生这种情况?如果我移动顶部的第二个代码块(-re“Please enter the play name_type”),前两个将通过。原因是什么


似乎第三个代码块从未执行过,我在其中添加了一些跟踪,如下所示:
将“executed!!!!”
,消息从未显示,似乎它忽略了第三个代码块,并在整个expect块下执行了代码,如何修复它?

因为第一个模式是第二个模式的子字符串,第一个图案将匹配两次。使用
expect-re
时,通常建议匹配到行尾,因此

expect { 
    timeout { 
        puts "timeout error"
    }
    -re "Please enter the play name: $" {
        exp_send $common1
        exp_continue
    }
    -re "Please enter the play name_type: $" {
        exp_send $common2
        exp_continue
    }
    -re "Now you can get the information of the play.*\r" 
}
# continue processing here
我假设问题以冒号、空格和行尾结尾。相应地调整


我通常的expect建议:在调试或开发程序时,将
exp_internal 1
添加到脚本顶部。

谢谢,但为什么第一个模式会匹配两次?在我的代码中,第一个块是“请输入播放名称”,因此代码卡在这里,等待expect_send$common1触发的输出,如果匹配,则退出第一个块,继续第二个块停留在-re“Please enter the play name_type”,并等待expect_send$common2触发的输出,为什么第一个模式将匹配两次?为什么第三个模式不被执行而被忽略?@ratzip Expect一次只匹配一个字符;当第一个模式匹配时,它不知道还会有更多。解决方法是强制模式仅在它是明确的一件事或另一件事时匹配。这也使您的代码更清晰,这是一个双重奖励。@DonalFellows我不明白,那里有三种模式,它将尝试同时匹配这些模式,屏幕上的第一个输出是“请输入播放名称”,第一个模式匹配,因此它发送common1,然后第二个输出是“请输入播放名称”\u type,它应该符合第二种模式,对吗?你是说它应该和第一个图案匹配?为什么?