tcl - how to find and replace sencond occurance of string using regsub -
tcl - how to find and replace sencond occurance of string using regsub -
i new tcl, trying learn, need help below. string looks in configfilebuf , trying replace sec occurance of confenb:local-udp-port>31001" xyz, below regsub cmd tried replacing first occurance (37896). plz help how replace sec occurance xyz.
set configfilebuf "<confenb:virtual-phy> </confenb:local-ip-addr> <confenb:local-udp-port>37896</confenb:local-udp-port> </confenb:local-ip-addr> <confenb:local-udp-port>31001</confenb:local-udp-port> </confenb:virtual-phy>" regsub -start 1 "</confenb:local-ip-addr>\[ \n\t\]+<confenb:local-udp-port>\[0-9 \]+</confenb:local-udp-port>" $configfilebuf "xyz" configfilebuf puts $configfilebuf
you have utilize regexp -indices
find start replacement, , regsub
. it's not bad if set regular look in own variable.
set re "</confenb:local-ip-addr>\[ \n\t\]+<confenb:local-udp-port>\[0-9 \]+</confenb:local-udp-port>" set start [lindex [regexp -all -indices -inline $re $configfilebuf] 1 0] regsub -start $start re $configfilebuf "xyz" configfilebuf
the 1
number of submatches in re (zero in case) plus 1. can compute help of regexp -about
, giving piece of trickiness:
set re "</confenb:local-ip-addr>\[ \n\t\]+<confenb:local-udp-port>\[0-9 \]+</confenb:local-udp-port>" set relen [expr {1 + [lindex [regexp -about $re] 0]}] set start [lindex [regexp -all -indices -inline $re $configfilebuf] $relen 0] regsub -start $start re $configfilebuf "xyz" configfilebuf
tcl
Comments
Post a Comment