From: Pit Capitain Date: 2002-04-27T00:30:39+09:00 Subject: Re: expand_path and overlap On 26 Apr 2002, at 23:08, nobu.nokada@softhome.net wrote: > At Fri, 26 Apr 2002 22:42:03 +0900, > Morris, Chris wrote: > > > I find this an interesting puzzle. Question: what would this > > > return: > > > > > > collapse_path("b/c/f", "/a/b/c/d/e/b/c/g/h") > > > #^^^^^^^^^^^^ or whatever :-) > > > > At the moment, I'm thinking it should be: > > > > /a/b/c/d/e/b/c/f > > > > (which could probably be gathered from my other recent post :) > > I'm not sure, is this like what you want? > > def collapse_path(path, base) > head, = path.split("/", 2) > base.rindex(%r"(\A|.*/)#{Regexp.quote(head)}(?:/|\z)") > ($1 or base) + path > end Here are two other implementations (using only the basic regular expressions I can understand :-) def collapse_path1( path, base ) pattern = Regexp.quote( path.sub( /(.)\/.*/, '\1' ) ) base.dup.sub!( /(.*)#{pattern}(\/|$).*/, '\1' + path ) || path end def collapse_path2( path, base ) pattern = Regexp.quote( path.dup ) while pattern return $1 + path if base =~ /(.*)#{pattern}(\/|$)/ pattern.sub!( /(.*)\/.*/, '\1' ) end return path end collapse_path1( "/b/c/f", "/a/b/c/d/e/b/x/g/h" ) # => /a/b/c/d/e/b/c/f collapse_path2( "/b/c/f", "/a/b/c/d/e/b/x/g/h" ) # => /a/b/c/f Don't know which one you want. Regards, Pit