aotto1968 <
[email protected]> wrote:
# I have a question regarding *regsub* and how to accelerate replacement
# let's assume the following code:
set str "aaa123bbb123ccc123ddd123eee123fff123ggg"
# My goal is to eliminate the all "123" except the FIRST one with the restriction
# that between the "123" is *not* a number other then 123
puts [regsub -all {(\d+)([^\d]*)\1} $str {\1\2}]
aaa123bbbccc123dddeee123fffggg
# → my problem is that always the SECOND "111" is replaced because the replacement itself is *not*
# checked again.
That is correct, after first substitution of "123bbb123" to "123bbb",
then in the remainder it doesn't see the "ccc" wrapped in "123"s, so
cannot eliminate the trailing "123" for "ccc"
# my solution is a loop
while {[regsub -all {(\d+)([^\d]*)\1} $str {\1\2} str]} ""
I think this is the way to go, but you might experiment with
removing the "-all" option... Maybe it improves speed, or
maybe it spoils it, I can't predict.
# this works but the GOAL is to have ONE *regsub* to get this job done
puts $str
aaa123bbbcccdddeeefffggg
Another approach could be to extract the non-"123"s as a list
with regexp (not regsub), and then just re-insert the number:
set num [regexp -inline {\d+} $str];# get the separating number
set list [regexp -inline -all {\D+} $str] ;# \D is like [^\d]
puts [join [linsert $list 1 $num] ""]
(unless you also need to deal with aaa123bbb123ccc456ddd456 where
the first 456 also needs to stay...)
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)