Reputation: 1013
In Tcl, I want to split the buffer based on string.
i.e,
set buffer "abc def geh ijk lmn abc 123 rfs sdf abc asdfg sadfga"
Now I want to split the buffer based on the string say "abc" So my output should look like:
{ { def geh ijk lmn } { 123 rfs sdf } { asdfg sadfga}}
I have tried using,
set output [split $buffer "abc"]
But this splits the string based on all a,b,c characters separately.
Upvotes: 1
Views: 136
Reputation: 1
set buffer "abc def geh ijk lmn abc 123 rfs sdf abc asdfg sadfga"
regsub "abc" $buffer "\n" string
split $string "\n"
Upvotes: 0
Reputation: 137567
The split
command treats its second argument as a set of characters to split on. To get what you really want, you want splitx
from the textutil::split
package in Tcllib:
package require textutil::split
textutil::split::splitx $buffer "abc"
If you don't know if your real split string has RE meta characters in it (i.e., pretty much any non-alphanumeric), use the “this is really a literal” magic prefix:
textutil::split::splitx $buffer ***=$splitString
(In both cases above with your particular buffer string, you will also end up with an empty first item, indicating that the split term was at the front of the buffer.)
Upvotes: 5