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

I am trying to figure out how to use a string in a regular expression. I searched Google for an hour, thought I would just ask the experts.

It works:

#!/usr/bin/expect set MYSTR "value" if [ regexp -nocase "$MYSTR" $outcome matchresult ] then { ... } 

This does not work:

 #!/usr/bin/expect set MYSTR "value" if [ regexp -nocase {something here:\s+$MYSTR} $outcome matchresult ] then { ... } 

I am sure this is a simple syntax problem.

thanks for the help

+4
source share
1 answer

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

PS: put {} around the expression :

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

All Articles