foreach my $actual_path_to_check (keys(%{$virtual_paths_href})) { foreach my $current_actual_path (keys(%{$virtual_paths_href})) { next if ($actual_path_to_check eq $current_actual_path); if ($virtual_paths_href->{$current_actual_path} =~ /^$virtual_paths_href->{$actual_path_to_check}/) { # Replace } } }