in reply to Extract middle string from a path using RegExp

Surely you want a more generic solution than this:
use strict; use warnings; my $str = 'C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometest\t +est12.1x\1'; my ($str2) = $str =~ /(fldr3\\fldr4\\fldr5\\test.txt)/; print "str2 = $str2\n"; __END__ str2 = fldr3\fldr4\fldr5\test.txt

If so, please re-phrase your question in more generic terms.

Also, there is free documentation at: perlretut

Replies are listed 'Best First'.
Re^2: Extract middle string from a path using RegExp
by dani_cv (Acolyte) on Sep 06, 2008 at 01:41 UTC

    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?
      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

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