2015-01-07 37 views
1

的问题:有一个与标签(在胶乳)一个字符串,我需要只是内容替换\ textbf {内容}只使用TCL和regexps(我有TCL v8.4)。标签多次出现在一个字符串中。更换使用TCL(多个实例)标记{内容}其内容

所以,这里是我:

使用\ textbf {余弦}而不是\ textbf {正弦}功能是用于压缩关键,因为事实证明,\ textbf {需要较少的余弦函数来近似一个典型的信号}

下面是我想:

采用余弦而不是正弦功能是压缩至关重要的,因为事实证明,较少的余弦函数,需要以接近典型信号

据我所知,在I have to escape the special charactersregsub,但我无法找到如何做到这一点。

这是我到目前为止有:

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 
} 

是发现标记文本(在$垃圾)和无标签的文字($ needed_stuff),但不会取代它们。任何帮助是极大的赞赏。

回答

3

您正在寻找的关键是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 {\1}] 
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. 

它看起来像之类的事情,你的愿望。

+0

哦,就是这样!多谢,Donal! – virens