regex: remembering patterns as parameters

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
mark [ Mo, 12 September 2005 12:21 ] [ ID #963073 ]

Re: regex: remembering patterns as parameters

"Mark" <Scollop027 [at] gmx.net> wrote in message
news:dg3krr$rof$02$1 [at] 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:

<quote>
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.
</quote>

Matt
Matt Garrish [ Mo, 12 September 2005 13:15 ] [ ID #963074 ]
Perl » alt.perl » regex: remembering patterns as parameters

Vorheriges Thema: REGEX help needed on binary file
Nächstes Thema: What's wrong with this code?