简体   繁体   中英

how to remove characters in a string till the first occurrence of a string,

For ex=> set a 123/345/567/2345, now i want to remove 123 and keep rest

expected output is 345/567/2345

How to do this in tcl without the use of awk '{print $1}'

set string 123/456/789
set idx [string last "/" $string]
set wanted [string range $string $idx-3 end]

Saw this approach in one of the posts but i want to make it so that 12345/43444/567676 should give me output as 43444/567676

I think you need to use string first , not string last. And add 1 to idx to ignore the leading forward slash.

set string 12345/43444/567676
set idx [string first "/" $string]
set wanted [string range $string $idx+1 end]

Outputs:

43444/567676

How about this:

set input "123/345/567/2345"
regexp {^[^/]*/(.*)$} $input matched output
puts $output  ;# 345/567/2345

Here's another way using list operations:

(Tcl) 1 % set string 123/456/789
123/456/789
(Tcl) 2 % set list [split $string / ]
123 456 789
(Tcl) 3 % set l2 [lassign $list dummy]
456 789
(Tcl) 4 % set wanted [join $l2 / ]
456/789

Or the same thing as a one-liner:

set wanted [join [lassign [split $string / ] dummy] / ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM