Search code examples
stringsubstringtcl

Substring extraction in TCL


I'm trying to extract a sequence of characters from a string in TCL.
Say, I have "blahABC:blahDEF:yadamsg=abcd".
I want to extract the substring starting with "msg=" until I reach the end of the string.
Or rather I am interested in extracting "abcd" from the above example string.
Any help is greatly appreciated.
Thanks.


Solution

  • Another approach: split the query parameter using & as the separator, find the element starting with "msg=" and then get the text after the =

    % set string blahblah&msg=abcd&yada
    blahblah&msg=abcd&yada
    % lsearch -inline [split $string &] {msg=*}
    msg=abcd
    % string range [lsearch -inline [split $string &] {msg=*}] 4 end
    abcd