in reply to Re^2: Extract middle string from a path using RegExp
in thread Extract middle string from a path using RegExp
Something has to be static, otherwise you just don't know what to do. How do you decide which components you want?
Is it the amount of directories up front? If so, first shove off the trailing thing - that's '@@' and what follows, right? Then use File::Spec->splitdir to split the path into components. Remove leading directories. Join the list with File::Spec->catdir.
use File::Spec; my $goners = 3; # how many? my $str = 'C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometes +t\test12.1x\1'; $str =~ s/\@/\@.*//; my @dirs = File::Spec->splitdir($str); splice @dirs, 0, $goners; print File::Spec->catdir(@dirs);
|
|---|