$text =~ s/%/%%/g; # replace first block with "(%1%)" # replace second block with "(%2%)" # ... my %subs= ( replaceThis => "withThis", # ... ); for( @subs{ keys %subs } ) { s/%/%%/g; } $text =~ s/$_/$subs{$_}/g for keys %subs; # replace (%1%) with original first block # ... $text =~ s/%%/%/g;