使用字符串变量定义表达式的Regexp函数行为



搜索文件内容以删除预编译指令之间带有$DEBUG字符的行。

搜索:

的典型数据模式
#IFDEF $DEBUG_MY_TEST
.... lines ...
#ENDIF

#IFDEF DEBUG_MY_issues for_sam
.... lines ...
#ENDIF
#IFDEF CMER for_max
.... lines ....
#ENDIF

这个表达式测试工作:

if { [regexp -lineanchor -nocase -- {^[ t]*#IFDEF[ t]+[$]?DEBUG.*} $oline_strpd ] == 1 } {
  set remove_to_cust_endif $oline_strpd; # sanity check
  continue;
}

我认为问题是在字符串变量模式中使用$字符。

使用此字符串变量方法进行搜索不工作?:

set RE_STRNG [format "{^[ \t]*#IFDEF[ \t]+[\$]?DEBUG.*}"]
if { [regexp -lineanchor -nocase -- $RE_STRNG $oline_strpd ] == 1 } {
  set remove_to_cust_endif $oline_strpd; # sanity check
  continue;
}

在前一行代码中,使用这个字符串变量方法是有效的:

set RE_STRNG [format "{^[ \t]*#IFDEF[ \t]+CMER[ \t]+%s}" $is_cmer_name ]; # insert name into the search pattern
if { [regexp -lineanchor -nocase -- $RE_STRNG $oline_strpd ] == 1 && [llength $oline_strpd] == 3 } {
      set print_to_cust_endif $oline_strpd; # sanity check
      continue;
}

嗯,因为你没有任何替换要做,你可以简单地把regexp本身。

如此:

set RE_STRNG {^[ t]*#IFDEF[ t]+[$]?DEBUG.*}
if { [regexp -lineanchor -nocase -- $RE_STRNG $oline_strpd ] == 1 } {
    set remove_to_cust_endif $oline_strpd; # sanity check
    continue;
}

或者如果你想使用format,你仍然可以保留大括号:

set RE_STRNG [format {^[ t]*#IFDEF[ t]+[$]?DEBUG.*}]
if { [regexp -lineanchor -nocase -- $RE_STRNG $oline_strpd ] == 1 } {
    set remove_to_cust_endif $oline_strpd; # sanity check
    continue;
}

我不知道为什么它不工作,但以下工作:

set RE_STRNG [format "^[ \t]*#IFDEF[ \t]+[\$]?DEBUG.*"]
if { [regexp -lineanchor -nocase -- $RE_STRNG $oline_strpd ] == 1 } {
    set remove_to_cust_endif $oline_strpd; # sanity check
    continue;
}

我测试了一些更有趣的是,如果你在文件的某个地方插入[format "{^[ \t]*#IFDEF[ \t]+[\$]?DEBUG.*}"],你有行,并使用这个:

set RE_STRNG [format "{.*}"]
if { [regexp -lineanchor -nocase -- $RE_STRNG $oline_strpd ] == 1 } {
    set remove_to_cust_endif $oline_strpd; # sanity check
    continue;
}

唯一匹配的行是您刚刚插入的行,这使我相信正则表达式正试图匹配文本中的[format "{.*}"](使用格式)。所以我相信,由于替换,Tcl在将其放入regex之前执行命令format,而没有它,它是在regex中使用格式命令插入整个内容。

最新更新