user2966788
user2966788

Reputation: 21

How to get whole string after first slash(/) using regexp in tcl

I have been trying to get all the characters after the first slash using regexp in Tcl.

What is want is this:

abc/def/ghi

from the above string i want def/ghi.

I tried using the below command, but its only giving ghi

set abc [regexp {([^/]*)$} $string match]

Upvotes: 2

Views: 7284

Answers (3)

Donal Fellows
Donal Fellows

Reputation: 137577

The simplest way of getting the value is this:

regexp {/(.*)} $string -> match

The command will assign the string you want to match, and will (effectively) produce a boolean as a result indicating whether the RE matched at all (i.e., was there a / in the string), allowing you to detect if the input was bogus. No tail-anchoring of the RE is necessary; Tcl's RE engine will greedily consume from the first / to the end of the string.

Upvotes: 1

Johannes Kuhn
Johannes Kuhn

Reputation: 15163

Why use a regexp for this simple case?

Just split the string at the first occurrence of /:

set str "abc/def/ghi"
string range $str [string first "/" $str]+1 end

Upvotes: 1

matt forsythe
matt forsythe

Reputation: 3912

I think the expression you want is /(.*)$, and then grab cluster group 1 as your result.

Upvotes: 3

Related Questions