im looking for a way to replace a certain value with almost the same value, the difference is that the new value is the same text, only different colours.
eg:
list that contains A B C D E
want to replace lindex $list 2 (so letter C) with the same value (letter C ^^) again but also coloured. somehow i ran out of ideas, please help
Last edited by raider2k on Wed Jul 08, 2009 9:09 am, edited 1 time in total.
With regsub, "&" within the subSpec parameter means "insert whatever matched exp here".
You may certainly create the regular expression to be used on the fly, although you'll need to care for a proper expression (having a value of .* would most certainly not generate the result you intended), as regsub uses regular expression pattern-matching rather than literal string-matching.
I'm not sure why there's a subst in there for starters though, especially since the evaluation is blocked using {}. I'd rather suggest building a proper regular expression by hand.
One word of caution, whenever you use regsub on a list, the output technically is no longer a list, but a string. With some friendly modifications, the list structure would probably still be valid, but it cannot be guaranteed. The same goes for string map.
Saying which approach you should use is rather hard with the limited description given. However, if you need pattern-based substitutions, regsub is your friend. If you're rather looking for a single list item, or mere literal strings to be replaced, you'd be better off using other approaches.
The subst command was there to force substitution of the variable name by it's value. Sticking to the convention of surrounding the regsub pattern with braces would otherwise have prevented substitution.
arfer,
The practice of enclosing patterns with {} is mainly due to the fact that regular pattern makes use of [] and \. If you store your expression in a variable, there would be no use for {}. Also, with proper escaping, you could simply use "" as well. This is nothing unique for regsub, but in any case where you'd have to have one []\ or similar within your string.
Below you'll find a few examples each doing the very same substitutions, having the exact same expressions, strings and subspecs...
set somestring "Hello world!"
#set somestring {Hello world!}
#Works well, might look a little messy...
regsub -- "\[a-zA-Z0-9\]+" $somestring "\00304\\0\003"
#Works well, considered the preferred way when including the exp and subSpec literally into the commandline
regsub -- {[a-zA-Z0-9]} $somestring "\00304&\003"
#Works well, separates the exp and subSpec from the command
set exp {[a-zA-Z0-9]}
#set exp "\[a-zA-Z0-9\]"
set subSpec "\00304&\003"
regsub -- $exp $somestring $subSpec
#Overworked, and looks messy... still works though...
regsub -- [subst {$exp}] [subst {$somestring}] [subst {$subSpec}]
guys, thanks very much to both of you since you helped me out of what i got stuck with and also i now experienced some answers regarding regsub i was looking for before