5

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

I want help in passing varibles to a regexp.

Suppose my code is

set line "MPID:22 condition:AIS"
set id 22
if {[regexp {MPID:$id} $line]} {
puts "inside if"
}

This regexp doesn't work. If I change regexp to

{[regexp {MPID:22} $line]} 

it works.

Can someone provide a solution for this.

4

1 回答 1

11

而不是{MPID:$id}你想使用"MPID:$id"

if {[regexp "MPID:$id" $line]} {
    puts "inside if"
}

tcl使用{...}它们将表达式的各个部分组合在一起,但防止变量扩展。
如果你想要变量扩展,你应该使用"..."

于 2012-08-31T12:07:10.393 回答