期望中的正则表达式



我对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"请输入播放name_type")。 为什么会这样? 如果我移动顶部的第二个代码块(-re"请输入播放name_type"),前两个就会通过。 原因是什么?

并且似乎第三个代码块从未执行过,我在里面添加了一些跟踪,如下所示:puts "executed!!!!",消息从未显示,似乎忽略了第三个代码块并执行了整个期望块下的代码,如何解决?

由于第一个模式是第二个模式的子字符串,因此第一个模式将匹配两次。使用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

我假设问题以冒号、空格和行尾结尾。相应地调整。

我通常的期望建议是:当你调试或开发程序时,在脚本的顶部添加exp_internal 1

最新更新