How to use a variable in regexp expression (TCL/Expect)

℡╲_俬逩灬. 提交于 2019-12-19 08:50:35

问题


I'm trying to figure out how to use a string in a regexp match. I have been searching on google for an hour, figured i would just ask the experts.

This works:

#!/usr/bin/expect

set MYSTR "value"

if [ regexp -nocase "$MYSTR" $outcome matchresult ] then {
...
}

This is not working:

#!/usr/bin/expect

set MYSTR "value"

if [ regexp -nocase {something here:\s+$MYSTR} $outcome matchresult ] then {
...
}

I'm sure it's a simple syntax problem.

Thanks for your help


回答1:


Right. You have 2 options: enclose the pattern with " , but then you have to protect \ from being parsed by Tcl instead of the regxp. Or you can use regexp -nocase [subst -nocommands -nobackslashes {something here:\s+$MYSTR}].

PS: put always {} around the expression:

if {[regexp -nocase [subst -nocommands -nobackslashes {something here:\s+$MYSTR}]} then {
...
}


来源:https://stackoverflow.com/questions/11768739/how-to-use-a-variable-in-regexp-expression-tcl-expect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!