in reply to Re: Extract middle string from a path using RegExp
in thread Extract middle string from a path using RegExp


my $str = 'C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometest\t +est12.1x\1';
Here
C:\fldr1\fld2\
is dynamic.
fldr3\fldr4\fldr5\test.txt
is also dyanamic.
Moreover,
@@\main\Msometest\t +est12.1x\1'
is dynamic.... Now how can I extract the middle path?
  • Comment on Re^2: Extract middle string from a path using RegExp

Replies are listed 'Best First'.
Re^3: Extract middle string from a path using RegExp
by ikegami (Patriarch) on Sep 06, 2008 at 02:04 UTC
    toolic's point is that it's impossible unless you tell us what special about the indicated points
    | | | | v v C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometest\test12.1x\1

    For instance, how do you know it's not

    | | | | v v C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometest\test12.1x\1

    or

    | | | | v v C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometest\test12.1x\1
Re^3: Extract middle string from a path using RegExp
by shmem (Chancellor) on Sep 06, 2008 at 10:41 UTC

    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);