2012-08-01 40 views
4

我想弄清楚如何在正则表达式匹配中使用字符串。我一直在google上搜索一个小时,我想问问专家。如何在正则表达式中使用变量(TCL /期望)

这工作:

#!/usr/bin/expect 

set MYSTR "value" 

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

这不是工作:

#!/usr/bin/expect 

set MYSTR "value" 

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

我敢肯定,这是一个简单的语法问题。

感谢您的帮助

回答

10

没错。你有2个选项:用"括住模式,但是你必须保护\免于被Tcl而不是regxp解析。或者你可以使用regexp -nocase [subst -nocommands -nobackslashes {something here:\s+$MYSTR}]

PS:将始终{}around theexpression

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

THANK YOU!谢谢! – Jared 2012-08-01 23:29:38