First, we get rid of /., because that won't take us anywhere. Next we loop while there's a dot followed by a forward slash. As /.. takes us up a directory, we look for a valid directory followed by that construct, and get rid of the whole thing. Eventually, the loop has to fail, and we must have come up with something.#!/usr/bin/perl -w use strict; my $path = "/foo/mik/../mik/./../mik"; # print "Path is --$path--\n"; $path =~ s!/\.([^.])!$1!g; # print "Path is now --$path--\n"; while ($path =~ m!/\.!) { $path =~ s!/[^\/]+/\.\.!!; # print "Path is now -+$path+-\n"; } print "Ended up with =>>$path<<=\n";
merlyn and turnstep are correct, though -- doing this sort of thing with regular expressions is pretty easy to break. Unless you have *complete* control of the directories being passed to your script and in the filesystem, don't use this.
In reply to Re: Normalized directory paths
by chromatic
in thread Normalized directory paths
by mikfire
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |