s{^[.](?=/root(?:/|$))}{} #### #!/usr/bin/env perl use strict; use warnings; use Test::More; my @tests = ( [qw{./root /root}], [qw{./root/ /root/}], [qw{./root/xyz /root/xyz}], [qw{./root_plus ./root_plus}], [qw{./not_root/ ./not_root/}], [qw{./script_not_in_path.pl ./script_not_in_path.pl}], [qw{.hidden .hidden}], ); plan tests => 0+@tests; for my $test (@tests) { my $file = $test->[0]; $file =~ s{^[.](?=/root(?:/|$))}{}; is $file, $test->[1]; } #### 1..7 ok 1 ok 2 ok 3 ok 4 ok 5 ok 6 ok 7