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?
set text "Some arbitrary text which might include \$ or {"
set wordList [regexp -inline -all -- {\S+} $text]
See this: Splitting a String Into Words.
Wow, I didn't know
regexp
could return a list. I would have done this, which is almost as good[split [regsub { {2,}} $string " "] " "]
. The regsub replaces all sequences of spaces of length 2 or more with a single space, then the split splits on that.set wordList [regexp -inline -all -- {\S+} $text] here what does "--" mean?
That's necessary in case the regex begins with a "-" character, plus it's just a good habit to get into (for this plus many Tcl commands where the first non-switch argument can be perhaps user-input:
file delete -- $file
,switch -exact -- $word
, ...)BTW see stackoverflow.com/questions/3369458/… and hume.com/html84/mann/regexp.html for -all and -inline flags.