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

Hi Monks,

still "only" a regexp question:

I have

$a='abab'; $b='a(.)'; $c='a$1'; print "\$a $a\n"; print "\$b $b\n"; print "\$c $c\n"; $a =~ s/$b/$c/g ; # so this is s/a(.)/a$1/g print "\$a $a\n";
which prints
$a abab $b a(.) $c a$1 $a a$1a$1
What I wanted/expected was the last line to be
$a abab
It's clear, after $c is evaluated to a$1 $1 isn't further evaluated to the content of the matching braces in the regexp.

How to change the above code so that the output is my expected one? Is it possible to combine this two different evaluations? Thanks for any help.

regards,
almaric

Replies are listed 'Best First'.
Re: regexp $1 in a variable
by Enlil (Parson) on Mar 02, 2004 at 03:51 UTC
Re: regexp $1 in a variable
by Stevie-O (Friar) on Mar 02, 2004 at 02:05 UTC
    $a =~ s/$b/qq[qq{$c}]/eeg
    Not sure exactly why the double-e (and double-qq) are needed, but what can I say -- it works!
    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
      Maybe this looks a bit cleaner :-)
      ... $c='"a$1"'; ... $a =~ s/$b/$c/eeg ; ...
Re: regexp $1 in a variable
by Roger (Parson) on Mar 02, 2004 at 00:36 UTC
Re: regexp $1 in a variable
by esskar (Deacon) on Mar 02, 2004 at 00:29 UTC
    change
    $b='\Qa(.)\E';
    and it will work


    UPDATE: wrong thought