使用tcl在两个变量上使用regexp

使用tcl在两个变量上使用regexp,tcl,Tcl,我有两个变量 echo $a >> 123.txt echo $b >> 341.txt 123.txt 654.txt 我想使用regexp来确定a是否存在于b中 您可以这样做: if {$a in $b} { puts present } else { puts absent } 中的如中所述 或者使用regexp,这是: if {[regexp -- "\\m[regsub -all {\W} $a {\\&}]\\M" [join $b

我有两个变量

echo $a
>> 123.txt
echo $b
>> 341.txt 123.txt 654.txt
我想使用regexp来确定a是否存在于b中

您可以这样做:

if {$a in $b} {
    puts present
} else {
    puts absent
}
中的
如中所述

或者使用regexp,这是:

if {[regexp -- "\\m[regsub -all {\W} $a {\\&}]\\M" [join $b]]} {
    puts present
} else {
    puts absent
}

所有这些复杂问题都是为了确保您不匹配“123 txt”或“0123.txt”或“123.txt2”

Tcl的
regexp
让我们在正则表达式中提供一个特殊前缀,表示RE的其余部分是文本字符串。这样做很容易(前提是您不希望锚定):


***=
前缀(如果存在)必须是RE的前四个字符。

为什么要使用regexp?在什么情况下,标准或标准是不够的?
if {[regexp ***=$a $b]} {
    puts "found!"
}