my @matches = $loop_line =~/\[(.*?)\]/; # Build a list of matches for ( @matches ) { $loop_line =~ s/\Q$_\E/$replace{$_}/; # \Q makes sure no strange things happen }