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

使用字符串变量定义表达式的regexp函数行为,regex,tcl,Regex,Tcl,搜索文件内容以删除具有$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字符的预编译指令之间的行

数据线搜索的典型模式:

#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;
}
或者,如果要使用
格式
,仍可以保留大括号:

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;
}
我做了更多有趣的测试,如果您在文件中的某个地方插入
[格式“\{^\[\\t\]*\\\[\\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在将其放入正则表达式之前执行命令
format
,而没有它,它将使用format命令在正则表达式中插入所有内容。

您使用的语言是什么?请格式化你的问题。当你写问题时,你看到了格式化文本的预览。不要提交问题,直到它看起来像是你想要的。您还可以在右侧找到“标记编辑帮助”按钮(带问号的橙色按钮)和“如何格式化”框。请阅读此信息,了解如何格式化您的问题,特别是代码,以便其他人能够阅读和理解问题并帮助您。Jerry-谢谢你的评论。@user2406910你能用这个解决你遇到的问题吗?谢谢你的帮助,这是我的解决方案-我的解决方案基于上面的帮助:function getUnits(num){var units=null;numprt=num.replace(/[A-z]+$/,“”);if(num.length!=numprt.length){units=num.replace(numprt,“”;}返回(单位);}