regex: remembering patterns as parameters

regex: remembering patterns as parameters

am 12.09.2005 12:21:47 von mark

hello,

I want to give a function a part of a regex which contains "\1" but this is
interpreted as string and not as a remembering pattern. How can I do sth.
like this:

$replace = "\2" ;
$val =~ s/$search/$replace/ ;

thanks and regards
Mark

Re: regex: remembering patterns as parameters

am 12.09.2005 13:15:23 von Matt Garrish

"Mark" wrote in message
news:dg3krr$rof$02$1@news.t-online.com...
> hello,
>
> I want to give a function a part of a regex which contains "\1" but this
> is
> interpreted as string and not as a remembering pattern. How can I do sth.
> like this:
>
> $replace = "\2" ;

Don't use double quotes around strings unless you want the interpolation.
Also, see perlre for why \2 should be $2.

> $val =~ s/$search/$replace/ ;
>

From perlop:


A /e will cause the replacement portion to be treated as a full-fledged Perl
expression and evaluated right then and there. It is, however, syntax
checked at compile-time. A second e modifier will cause the replacement
portion to be evaled before being run as a Perl expression.


Matt