Re: string handle problem
- From: "Michael A. Cleverly" <michael@xxxxxxxxxxxx>
- Date: Mon, 28 Nov 2005 22:57:47 -0700
On Mon, 28 Nov 2005, Hans wrote:
Hi, I encountered a string handle problem and need help. In my code, I have a string which value could be "a1/a2" or "a2/a3/a4" or "a2/b3/c3/a5", now I hope to get the last sub_string after "/", for "a1/a2", I need "a2", for "a2/a3/a4" , I need "a4", and for "a2/b3/c3/a5", I need a5.
set last_sub_string [lindex [split $string /] end]
I knew I can use split command and then handle the list to get the last one, but it seems a little complex, Is there an easy way to get it?
proc last_sub_string {string {delimiter /}} { return [lindex [split $string $delimiter] end] }
Then just call [last_sub_string $string] when you need the value.
I'm thinking to use regexp, but regexp handle string in order from left to right by default, I don't know how to change it to 'from right to left'.
You can have the regular expression pattern anchored to match the end of the string with $. Two possibilities:
regexp {([^/]*)$} $string => last_sub_string
set last_sub_string [regexp -inline {[^/]*$} $string]Michael .
- References:
- string handle problem
- From: Hans
- string handle problem
- Prev by Date: Re: string handle problem
- Next by Date: Re: Getting system uptime on Windows
- Previous by thread: Re: string handle problem
- Next by thread: Expect timeout problem. I needs help.
- Index(es):