如何在regexp表达式中使用变量(TCL/Expect)

Jar*_*red 4 regex tcl expect

我试图弄清楚如何在正则表达式匹配中使用字符串.我一直在谷歌搜索一个小时,我想我会问专家.

这有效:

#!/usr/bin/expect

set MYSTR "value"

if [ regexp -nocase "$MYSTR" $outcome matchresult ] then {
...
}
Run Code Online (Sandbox Code Playgroud)

这不起作用:

#!/usr/bin/expect

set MYSTR "value"

if [ regexp -nocase {something here:\s+$MYSTR} $outcome matchresult ] then {
...
}
Run Code Online (Sandbox Code Playgroud)

我确定这是一个简单的语法问题.

谢谢你的帮助

Joh*_*uhn 11

对.你有两个选择:用模式包围模式",但是你必须保护它\不被Tcl而不是regxp解析.或者你可以使用regexp -nocase [subst -nocommands -nobackslashes {something here:\s+$MYSTR}].

PS:总是{} 把 表达式放在:

if {[regexp -nocase [subst -nocommands -nobackslashes {something here:\s+$MYSTR}]} then {
...
}
Run Code Online (Sandbox Code Playgroud)