for (values %copy) { 1 while # repeat till no replacement left s/^($pattern)/$h_orig->{$1}/g; } #### use strict; use warnings; use Data::Dump qw/pp dd/; my $h_orig = { '/a/b/c/dt/data/SUSE/tool/0.9.0' => '/a/b/c/dt/tools/SUSE/tool/0.9.0', '/nfs/us' => '/usr/bin', '/a/b/c' => '/nfs/us' }; my $pattern = join "|", map quotemeta, keys %$h_orig; my %copy = %$h_orig; for (values %copy) { 1 while s/^($pattern)/$h_orig->{$1}/g; } pp $h_orig, \%copy; #### -*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Sun Feb 6 15:59:06 C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/replace_pat2.pl ( { "/a/b/c" => "/nfs/us", "/a/b/c/dt/data/SUSE/tool/0.9.0" => "/a/b/c/dt/tools/SUSE/tool/0.9.0", "/nfs/us" => "/usr/bin", }, { "/a/b/c" => "/usr/bin", "/a/b/c/dt/data/SUSE/tool/0.9.0" => "/usr/bin/dt/tools/SUSE/tool/0.9.0", "/nfs/us" => "/usr/bin", }, ) Compilation finished at Sun Feb 6 15:59:06