Tcl 正则表达式变量不匹配,如何修复?

问题描述 投票:0回答:1

无法让变量在正则表达式中工作

尊敬的专家,

我试图在 tcl 的正则表达式中使用一个变量,但无法让它工作。在输入数据和正则表达式模式中,我尝试使用

set line_pattern ac_E13Q_DFLP_B_eqCcell_10_022
set cell_pattern ac_E13Q_DFLP_B_eqCcell

regexp {^"${cell_pattern}".+} $line_pattern match 

有人可以帮我让它工作吗?

regex tcl
1个回答
0
投票

Tcl 中的大括号就像 shell 中的单引号:变量不会在其中被替换。

regexp "^$cell_pattern.+" $line_pattern match 

您的模式不是正则表达式。您可能只想检查字符串是否以前缀开头。

if {[string first $cell_pattern $line_pattern] == 0} {
    puts "starts with!"
}
# or
if {[string match "${cell_pattern}*" $line_pattern]} {
    puts "starts with!"
}

文件:

© www.soinside.com 2019 - 2024. All rights reserved.