Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

expect

tcl

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

like image 693
Jared Avatar asked Dec 21 '22 18:12

Jared


1 Answers

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 {
...
}
like image 96
Johannes Kuhn Avatar answered Feb 12 '23 11:02

Johannes Kuhn