Note that if I were doing this for real I would not use a regex match to accomplish it but would use split for brevity or a module to cover edge cases and portability. However, here is one solution. This might not achieve what you want because it isn't clear precisely what you do want. It should provide a starting point at least.

use strict; use warnings; use Test::More; my @tests = ( { in => ' /x/y/z/a/b/c/d/e.f', want => '/c/d/e.f' } ); plan tests => scalar @tests; my $re = qr#((?:/[^/]*?){3})$#; for my $t (@tests) { $t->{in} =~ $re; is $1, $t->{want}, "Extracted $t->{want}"; }

🦛


In reply to Re: Regex to extract last 3 components of filename by hippo
in thread Regex to extract last 3 components of filename by mjurgens

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.