Felipe not looking for /foo/bar/baz, /bar/baz, /baz
but for /foo, /foo/bar, /foo/bar/baz
One solution building on regex idea in comments but give the right strings:
reverse the string to be matched:
xuuq/zab/rab/oof/
For instance in PHP usestrrev($string )
match with
(?=((?<=/)(?:\w+/)+))
This give you
zab/rab/oof/
rab/oof/
oof/
Then reverse the matches with strrev($string)
This give you
/foo/bar/baz
/foo/bar
/foo
If you had .NET not PCRE you could do matching right to left and proably come up with same.