listsplitwhitespacetcl

TCL - split string by arbitrary number of whitespaces to a list


Say I have a string like this:

set str "AAA    B C     DFG 142               56"

Now I want to get a list as follows:

{AAA B C DFG 142 56}

For that I want to use split function, but in that case I get some extra empty lists {}. How I can get the list above?


Solution

  • set text "Some arbitrary text which might include \$ or {"
    set wordList [regexp -inline -all -- {\S+} $text]
    

    See this: Splitting a String Into Words.