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