$str = "%tag1%ALONGSTRINGOFTEXT%tag2% #with $str = ~ s/\%(\S+)\%/$template{$1}/gi; #would yield: #$str eq $template{"tag1%ALONGSTRINGOFTEXT%tag2"};