SPUG: regexp s/// issues with backreferences stored in a scalar string

mike mike206 at gmail.com
Wed Feb 1 13:35:15 PST 2006


oh so i am trying to store a regexp substitution with back references in a
few variables and have them interpolate the $1, $2, ....

i tried it with the \1 , \2 notation as well with no luck. hrmph.

On 2/1/06, mike <mike206 at gmail.com> wrote:
>
> the comments pretty much speak for themselves.
> i did that so anyone interested can replicate the problem with a cut and
> paste.
>
> ---- begin ----
>
> #!/usr/bin/perl
> -w
>
>
> # here's a
> regexp
>
> my $regexp = q{(abcd)(efg)};
>
> # here's a substitution
> pattern
>
> # note the single q{} to block it from
> interpolating
>
> my $suss = q{$2  $1};
>
> # here's a
> string
>
> my $string = q{abcdefgh};
>
> # run the regexp and
> putout
>
> $string =~ s/$regexp/$suss/;
> print $string."\n";
>
> ############################ prints  '$2  $1h'
>
> # now try again but hardcode the backreferences in the substitution
>
> $string = q{abcdefgh};
> $string =~ s/$regexp/$2  $1/;
> print $string."\n";
> ################### prints 'efg  abcdh' , as it should
>
>
>
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
>     POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>    MEETINGS: 3rd Tuesdays
>    WEB PAGE: http://seattleperl.org/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20060201/49171cf5/attachment.html


More information about the spug-list mailing list