$filename =~ s/\/[\/]+/\//; #### # Used a delimiter other than '/' to avoid escaping '/'. $p =~ s!//+!/!g; #### $p = '/vobs/synergy_core_apps/code/../code/personalize/src'; print 'before: ' , $p , "\n"; # Besides the /g flag, used /x in order to make the pattern stand out. $p =~ s{ ([^/]+) /[.]{2}/ \1 } /$1/gx; print 'after: ' , $p , "\n";