Re: string handle problem
- From: Bryan Oakley <oakley@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 03:02:41 GMT
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.
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? 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'.
Thanks a lot.
If it were me I'd probably use split. I don't quite understand the remark about it being complex: 'set data [lindex [split $data /] end]' looks pretty simple to me. You can also use 'string last' to find the last occurance of "/", then grab all the chars after that with 'string range'
If you want to use regexp you don't have to use right-to-left matching. Just match the longest pattern before the /, the /, then everything after it. Perhaps {.*/([^/]+)$} or {/([^/]+)$} would give you the results you are looking for.
This data wouldn't actually be filenames would it? If so, you might look at the various 'file' subcommands.
.
- References:
- string handle problem
- From: Hans
- string handle problem
- Prev by Date: Re: string handle problem
- Next by Date: Expect timeout problem. I needs help.
- Previous by thread: Re: string handle problem
- Next by thread: Re: string handle problem
- Index(es):
Relevant Pages
|