Diego Henrique
Diego Henrique

Reputation: 255

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

I need a tip, tip or suggestion followed by some example of how I can add an extension in .txt format after the last character of a variable's output line.

For example:

set txt " ONLINE ENGLISH COURSE - LESSON 5 "

set result [concat "$txt" .txt]

Print:

enter image description here

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

With the built-in concat method of Tcl, it does not achieve the desired effect.

The expected result was something like this:

ONLINE ENGLISH COURSE - LESSON 5.txt

I know I could remove spaces with string map but I don't know how to remove just the last occurrence on the line.

And otherwise I don’t know how to remove the last space to add the text [.txt]

If anyone can point me to one or more solutions, thank you in advance.

Upvotes: 0

Views: 239

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

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

or

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

Upvotes: 3

Related Questions