For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > September 2005 > regex: remembering patterns as parameters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regex: remembering patterns as parameters
Mark

2005-09-12, 7:55 am

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


Matt Garrish

2005-09-12, 7:55 am


"Mark" <Scollop027@gmx.net> 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:

<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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com