【问题标题】:Simple regular expression not working in expect简单的正则表达式在期望中不起作用
【发布时间】:2019-09-29 12:18:52
【问题描述】:

我的expect 脚本很简单

#!/usr/bin/expect

set timeout 60
spawn docker run -it ubuntu:19.04
expect {
  -re "root@[0-9]+:\/#"
}
send -- exit
expect eof

由于

而失败
$ expect script.expect
spawn docker run -it ubuntu:19.04
invalid command name "0-9"
    while executing
"0-9"
    invoked from within
"expect {
  -re "root@[0-9]+:\/#"
}"
    (file "script.expect" line 5)

我希望这会成功,因为它是一个有效的 Perl 正则表达式,或者会失败并显示一条解释正则表达式错误的消息。似乎-re 正在尝试做其他事情而不是匹配指定的 re 这没有任何意义。

我试图逃避一切我能在 re 中猜到的东西,结果没有改变。

我在 Ubuntu 19.04 上使用 expect 5.45.4。

【问题讨论】:

  • expect 不使用 Perl。 (我认为它使用 TCL?)无论如何,它看起来需要一个 POSIX 正则表达式,因为我必须切换到 \[0-9\] 以匹配 01
  • 期望,作为 Tcl 扩展,会想要一个Tcl regex

标签: regex expect


【解决方案1】:

Expect 是 Tcl 的扩展。 Tcl 使用方括号进行命令替换。

此外,Tcl 就像 shell 一样,它允许在双引号内替换命令和变量。 Tcl 的非插值引号是花括号(类似于 shell 的单引号)。

https://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm

所以,您想将正则表达式括在大括号中:

expect -re {root@[0-9]+:/#}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    相关资源
    最近更新 更多