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}"; }