That string looks like it's actually not a string but a URI. So, let's treat it as one:
require 'uri'
uri = URI.parse(str)
Now, extracting the path component of the URI is a piece of cake:
path = uri.path
Now we have already greatly limited the amount of stuff that can go wrong with our own parsing. The only part of the URI we still have to deal with, is the path component.
A Regexp
that matches the part you are interested in looks like this:
%r|/to/\w+/(.*/)t$|i
If we put all of that together, we end up with something like this:
require 'uri'
def URI.extract(uri)
return parse(uri).path[%r|/to/\w+/(.*/)t$|i, 1]
end
require 'test/unit'
class TestUriExtract < Test::Unit::TestCase
def test_that_the_path_gets_extracted_correctly
uri = 'http://linkto.com/to/1pyTZl/somesite.com/2009/10/monit-on-ubuntu/t'
path = 'somesite.com/2009/10/monit-on-ubuntu/'
assert_equal path, URI.extract(uri)
end
end