TCL - split string by arbitrary number of whitespaces to a list
26,536
Solution 1
set text "Some arbitrary text which might include \$ or {"
set wordList [regexp -inline -all -- {\S+} $text]
See this: Splitting a String Into Words.
Solution 2
You can always do the following:
set str "AAA B C DFG 142 56"
set newStr [join $str " "]
It will output the following:
{AAA B C DFG 142 56}
Solution 3
The textutil::split module from tcllib has a splitx
proc that does exactly what you want
package require textutil::split
set result [textutil::split::splitx $str]
Solution 4
As of Tcl 8.5, the following also works:
list {*}$str
(provided the string is also a proper list, as in the question). The output is the desired list.
Documentation: list, {*} (syntax)
Comments
-
Narek over 3 years
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?
-
drysdam about 13 yearsWow, 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. -
Narek about 13 yearsset wordList [regexp -inline -all -- {\S+} $text] here what does "--" mean?
-
Narek about 13 yearsWhy does it work? As I know join - Create a string by joining together list elements, but your output is a list? and your input not a list... It is strange...
-
Scott about 13 yearsString, list, its all the same thing (technically). The output that was given, can easily be split into separate list elements, etc. You want your output to be a list though, don't you?
-
RHSeeger about 13 yearsThis works because the input can be parsed as a valid list. Had the input been "AAA }B C DFG 142 56", it would fail because that can't be parsed as a list.
-
glenn jackman about 13 yearsThat'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
, ...) -
Narek about 13 yearsBTW see stackoverflow.com/questions/3369458/… and hume.com/html84/mann/regexp.html for -all and -inline flags.