TCL--Regexp匹配不正确

问题描述 投票:0回答:1
set cell "HEADBUFTIE42D_D3_N"
set postfix "_M7P5TR_C60L08"
set line "  cell(HEADBUFTIE42D_D3_N_M7P5TR_C60L08) { "
if {[regexp "^ +cell[(]$cell$postfix[)] *\\{" $line match]} {
    puts "hello"
}

在这里我试图匹配行

  cell(HEADBUFTIE42D_D3_N_M7P5TR_C60L08) { 

注意,开头有2个空格,后面有1个空格。{.

但是匹配不发生。请帮助

regex syntax tcl
1个回答
1
投票

你的问题是没有正确地转义regexp元字符 ({ 逃不掉,那么 [(] 正在试图执行一条名为 (,等等)。) 如果你使用 format 像这样。

set cell "HEADBUFTIE42D_D3_N"
set postfix "_M7P5TR_C60L08"
set line "  cell(HEADBUFTIE42D_D3_N_M7P5TR_C60L08) { "
set re [format {^ +cell\(%s%s\) *\{} $cell $postfix]
if {[regexp $re $line match]} {
    puts "hello"
}
© www.soinside.com 2019 - 2024. All rights reserved.