To answer your specifc example..
$file =~ s%/[^/]+/\.\.$%%
... will eat the last directory name that's up'ed by the trailing ..
To give a more general/complete example that can handle multiple .'s and ..'s at any point in the path.
# colapse /./ and /.$
1 while ($file =~ s%/\.(/|$)%$1%g);
# colapse /dir/../ and /dir/..$
# ignore /../../ and /../..$
1 while ($file =~ s%/[^/]+/(?<!/\.\./)\.\.(/|$)%$1%g);
# remove ^dir/../ and ^dir/../
# ignore ^../../ and ^../..
1 while ($file =~ s%^[^/]+/(?<!^\.\./)\.\.(/|$)%%);
# colapse ^/../ to /
1 while ($file =~ s%^/\.\./%/%);
# expand null to .
$file =~ s%^$%.%;
We need the itterations of each loop as each substitution may reveal additional matchs.
We need multiple expressions to get the order of susbistion right. That is /../../ should colapse the two proceding directoires and not itself.
I'm sure it can be reduced more than that but it is as simple as I can see it can be and still catch all the special cases.
I use something very similar to this at the beginning of my scripts to get the conical path for $0 and not the called path. Although spliting the path into its components and working backwards is probably easier/clearer than using a regex. |