http://www.perlmonks.org?node_id=741871

arekfu has asked for the wisdom of the Perl Monks concerning the following question:

O wise Perl monks,

I have written a script that parses a file and applies a number of text substitutions. I have a dictionary (let's call it %patdic) of search/substitute pairs and, for each input text line, I run something like

foreach my $search (keys %patdic) { s/$search/$patdic{$search}/g; }

This works well as long as $patdic{$search} does not contain the $1 construct. Consider the following example:

#!/usr/bin/env perl use strict; use warnings; $_=q{nell'amaca}; my $search=q{nell'([[:alpha:]]+a)}; my $subst='na $1'; s/$search/$subst/g; print; print "\n";

This will return "na $1", while I would like it to return "na amaca". Any pointers, please?