in reply to Regex to extract last 3 components of filename

Because your regexp is greedy. See also Breaking file path into segments, perlretut.

Hope this helps!


The way forward always starts with a minimal test.
  • Comment on Re: Regex to extract last 3 components of filename

Replies are listed 'Best First'.
Re^2: Regex to extract last 3 components of filename
by Fletch (Bishop) on May 04, 2021 at 22:51 UTC

    Started to reply with a similar Path::Tiny invocation then read what you'd linked so . . . foo. Instead, File::Spec(::Functions):

    $ perl -MFile::Spec::Functions=splitdir -E '@p = splitdir( "/x/y/z/a/b +/c/d/e.f" );say join( qq{\n}, @p[-3,-2,-1] )' c d e.f

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.