aaa123bbbccc123dddeee123fffggg
aaa123bbbcccdddeeefffggg
# 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.
# my solution is a loop
while {[regsub -all {(\d+)([^\d]*)\1} $str {\1\2} str]} ""
# this works but the GOAL is to have ONE *regsub* to get this job done
puts $str
aaa123bbbcccdddeeefffggg
* aotto1968 <aotto1968@t-online.de>
| 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
If that *really* is the goal, I would simply search for the first "123"
and then [string map] the rest of them to "":
set str "aaa123bbb123ccc123ddd123eee123fff123ggg"
set res [string range $str 0 [string first 123 $str]+2]
append res [string map {123 ""} [string range $str [string first 123 $str ]+3 end]]
I do not completely understand the second part of the restriction, though...
R'
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 915 |
Nodes: | 10 (2 / 8) |
Uptime: | 46:17:56 |
Calls: | 12,170 |
Files: | 186,521 |
Messages: | 2,234,594 |