Tcl - 如何通过正则表达式在最后一个字符后添加文本?

Tcl - How to Add Text after last character through regex?

我需要提示、提示或建议,然后是一些示例,说明如何在变量输出行的最后一个字符后添加 .txt 格式的扩展名。

例如:

set txt " ONLINE ENGLISH COURSE - LESSON 5 "

set result [concat "$txt" .txt]

打印:

Note that there is space in the start, means and fin of the variable phrase (txt). What must be maintained are the spaces of the start and means. But replace the last space after the end of the sentence, with the format of the extension [.txt].

使用Tcl内置的concat方法,并没有达到预期的效果。

预期的结果是这样的:

ONLINE ENGLISH COURSE - LESSON 5.txt

我知道我可以用 string map 删除 space,但我不知道如何只删除行中最后一次出现的内容。

不然不知道怎么去掉最后的space添加文字[.txt]

如果有人能指出一种或多种解决方案,请提前致谢。

set result "[string trimright $txt].txt"

set result [regsub {\s*$} $txt ".txt"]