virens
virens

Reputation: 28

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

The problem: there is a string with tags (in LaTeX), and I need to replace \textbf{contents} with just contents using TCL and regexps only (I have TCL v8.4). The tag occurs multiple times in a string.

So, here is what I have:

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}.

Here is what I want:

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.

I understand that I have to escape the special characters in regsub, but I cannot find how to do this.

Here is what I have so far:

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
}

that finds the tagged text (in $trash) and the text without tags (in $needed_stuff), but does not replace them. Any help is greatly appreciated.

Upvotes: 1

Views: 67

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

The key things you are looking for are that the RE needs to be in {braces}, and the literal backslash and braces in the RE need to be backslash-quoted. You also want to use a non-greedy quantifier in there and the -all option to 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 {\1}]
puts $plain_text_contents

This produces this output:

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.

Which looks like the sort of thing you desire.

Upvotes: 3

Related Questions