Re: string handle problem



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.
.




Relevant Pages

  • Re: string handle problem
    ... I encountered a string handle problem and need help. ... >>>I knew I can use split command and then handle the list to get the last ... >>>I'm thinking to use regexp, but regexp handle string in order from left ... >I think it should work regardless of platform. ...
    (comp.lang.tcl)
  • Re: string handle problem
    ... I encountered a string handle problem and need help. ... >>I knew I can use split command and then handle the list to get the last ... >>I'm thinking to use regexp, but regexp handle string in order from left ... I think it should work regardless of platform. ...
    (comp.lang.tcl)
  • Re: string handle problem
    ... I encountered a string handle problem and need help. ... >I knew I can use split command and then handle the list to get the last ... >I'm thinking to use regexp, but regexp handle string in order from left ... This MIGHT be doable using the [file tail] ...
    (comp.lang.tcl)
  • Re: Regexp, ***= and subexpressions
    ... specified that the user's input be treated as a literal string rather than ... -exact] in cases where regexp isn't required:P ... Seems to be that regexp can't match word boundaries at "non-word" ... But now Earth has rotated and I can get back to sleep. ...
    (comp.lang.tcl)
  • Re: Regexp: Case-insensitive matching | N factorial
    ... a regexp component that matches a string of letters, ... Will match the string 'cat' anywhere it appears regardless of case. ... Javascript regular expressions have an alternative operator '|' (kind ...
    (comp.lang.javascript)