使用 TCL 将 \tag{contents} 替换为它的内容(多个实例)

Replace \tag{contents} with its contents using TCL (multiple instances)

问题:有一个带有标签的字符串(在 LaTeX 中),我需要将 \textbf{contents} 替换为仅 contents 使用 TCL 和正则表达式(我有 TCL v8.4)。标签在一个字符串中出现多次。

所以,这是我所拥有的:

The use of \textbf{cosine} rather than \textbf{sine} functions is critical for compression, since it turns out that \textbf{fewer cosine functions are needed to approximate a typical signal}.

这是我想要的:

The use of cosine rather than sine functions is critical for compression, since it turns out that fewer cosine functions are needed to approximate a typical signal.

我了解 regsub 中的 I have to escape the special characters,但我找不到如何执行此操作。

这是我目前的情况:

set project_contents {The use of \textbf{cosine} rather than \textbf{sine} functions is critical for compression, since it turns out that \textbf{fewer cosine functions are needed to approximate a typical signal}.}

set match [ regexp -all -inline  {\textbf\x7B([^\x7D]*)\x7D} $project_contents ]
foreach {trash needed_stuff} $match {

regsub -- {\textbf\{$trash\}} $project_contents   $needed_stuff    project_contents
}

找到带标签的文本(在 $trash 中)和没有标签的文本(在 $needed_stuff 中),但不替换它们。非常感谢任何帮助。

您正在寻找的关键是 RE 需要在 { 大括号 } 中,并且 RE 中的文字反斜杠和大括号需要反斜杠引用。您还想在那里使用非贪婪量词和 -all 选项 regsub:

set project_contents {The use of \textbf{cosine} rather than \textbf{sine} functions is critical for compression, since it turns out that \textbf{fewer cosine functions are needed to approximate a typical signal}.}
set plain_text_contents [regsub -all {\textbf\{(.*?)\}} $project_contents {}]
puts $plain_text_contents

这会产生以下输出:

The use of cosine rather than sine functions is critical for compression, since it turns out that fewer cosine functions are needed to approximate a typical signal.

这看起来正是您想要的那种东西。