2012-08-01 86 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