in reply to Path parsing from full to relative
Assuming that you want to exclude all directories above the lowest-level "working directory," so that for a path like
C:\foo\bar\ABCxyz\baz\boff\ABCxyz\myStuff.txt
you want
\ABCxyz\myStuff.txt
returned, and also making some assumptions about the whole ABC thing, this might do the trick:
c:\@Work\Perl\monks>perl -wMstrict -le "use List::Util qw(max); ;; my $rx_wordic = qr{ (?<= \\) ABC [^\\]* }xms; ;; my $maxlen = max map length, my @paths = qw( C:\somedirectory\anotherDirectory\ABCxyz\myFile.txt C:\somedirectory\anotherDirectory\ABCxyz\Logs\myLogs.txt C:\somedirectory\anotherDirectory\ABCxyz\Stuff\myStuff.txt C:\foo\bar\ABCxyz\baz\boff\ABCxyz\myStuff.txt C:\foo\bar\ABCxyz\baz\boff\ABCxyz\xyzzy.zy\a.b\myStuff.txt C:\myStuff.txt C:\totally\bogus\myStuff.txt ); ;; for my $path (@paths) { printf qq{%-*s -> }, $maxlen+2, qq{'$path'}; $path =~ s{ \A .* (?= $rx_wordic) }''xmsg; printf qq{%-s \n}, qq{'$path'}; } " 'C:\somedirectory\anotherDirectory\ABCxyz\myFile.txt' -> 'ABCxy +z\myFile.txt' 'C:\somedirectory\anotherDirectory\ABCxyz\Logs\myLogs.txt' -> 'ABCxy +z\Logs\myLogs.txt' 'C:\somedirectory\anotherDirectory\ABCxyz\Stuff\myStuff.txt' -> 'ABCxy +z\Stuff\myStuff.txt' 'C:\foo\bar\ABCxyz\baz\boff\ABCxyz\myStuff.txt' -> 'ABCxy +z\myStuff.txt' 'C:\foo\bar\ABCxyz\baz\boff\ABCxyz\xyzzy.zy\a.b\myStuff.txt' -> 'ABCxy +z\xyzzy.zy\a.b\myStuff.txt' 'C:\myStuff.txt' -> 'C:\my +Stuff.txt' 'C:\totally\bogus\myStuff.txt' -> 'C:\to +tally\bogus\myStuff.txt'
Update: Thanks to Laurent_R for making the point about avoiding a leading \ on processed paths (update: but this feature is easy to get rid of if you don't want it).
Give a man a fish: <%-{-{-{-<
|
|---|