0

我试图拥有一个非常通用的函数并命令它使用它从外部遇到的变量。我尝试了以下(简化代码),但没有用:

set line "found \$find1 at \$find2"
do_search $line

proc do_search {line} {
...
if {[regexp $exp $string match find1 find2} {
     puts "$line"
}

但是我得到的只是:found $find1 at $find2或者,如果我在调用函数之前不使用find\之前$find的值。

鉴于此正则表达式是解析文件时 while 循环的一部分,因此在调用 proc 后我无法使用这些值。

有什么想法怎么做?

4

1 回答 1

2

对于您的确切风格,您想要subst

if {[regexp $exp $string match find1 find2} {
     puts [subst $line]
}

但是您也可以考虑使用format

set fmt "found %s at %s"
do_search $fmt

proc do_search {fmt} {
...
if {[regexp $exp $string match find1 find2} {
     puts [format $fmt $find1 $find2]
}
于 2013-03-06T17:54:36.420 回答