Robert Klemme
6/28/2007 12:24:00 PM
On 28.06.2007 13:51, Lars Andren wrote:
> I've got a problem when trying to split a string after the thrid
> occurrence of a slash ('/'), I'm so used to java-thinking so I keep
> wanting to iterate through it and save the position of the third
> occurence and then cut the string at that index.
>
> This doesnt seem to be the Ruby-way, anyone got ideas of how I should do
> it?
Depends on what you want to do, i.e. which part(s) of the string you
need. You can do
irb(main):003:0> s = (1..5).to_a.join '/'
=> "1/2/3/4/5"
irb(main):008:0> %r<\A((?:[^/]*/){2}[^/]*)/(.*)\z> =~ s
=> 0
irb(main):009:0> pre, post = $1, $2
=> ["1/2/3", "4/5"]
irb(main):010:0>
Or
irb(main):011:0> s[%r<\A((?:[^/]*/){2}[^/]*)/>]
=> "1/2/3"
Or...
Kind regards
robert