in reply to Pass Matched Variable into Regex Replace Variable

You could do

... my $replace = '"To {$1}"'; $text =~ s/$pattern/$replace/gee; # eval twice

Replies are listed 'Best First'.
Re^2: Pass Matched Variable into Regex Replace Variable
by deMize (Monk) on Dec 17, 2010 at 18:29 UTC
    I feel like I've tried that and it didn't work.

    perl -e '$t="begin{foobar}";$p="begin{(.*?)}";$r="\"end{\$1}\""; $t=~s/$p/$r/gee; print "$t\n\n"'
    #Output: end{foobar}

    Perhaps I'm doing something wrong? Edit:I didn't escape the dollar sign in the variable. The suggestion works. I'll apply it to my program and see if everything works :) Thank you!
      $r="\"end{$1}\"";

      The double quotes interpolate the $1. Use $r=q("end{$1}") in this case.

        I actually did that in my production code, but it's worth mentioning for reference. :) cheers