Re: Question on regex substitution using variables...
- From: Jim Gibson <jgibson@xxxxxxxxxxxxxxxxx>
- Date: Thu, 02 Feb 2006 12:26:21 -0800
In article <pan.2006.02.02.18.23.39.191334@xxxxxxxxx>, Ben Bacarisse
<ben.usenet@xxxxxxxxx> wrote:
On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote:....
but where the two parts of the substitution are variables:
my $to_pattern = "(.*)\\.abc";
my $from_pattern = "\$1.def";
$f =~ s/$to_pattern/$from_pattern/;
print "$f\n";
Unfortunately this doesn't seem to work, where the first example correctly
prints out "fred.def" the second one doesn't seem to do the
back-substitution, and just prints "$1.def".
You need, in some sence, less quoting. One way is to re-write the
substitution as a little code block (in a string) and have it evaluated:
my $from_pattern = "\$1.'.def'";
$f =~ s/$to_pattern/eval($from_pattern)/e;
A second e modifier will force a second round of evaluation:
$f =~ s/$to_pattern/$from_pattern/ee;
This being Perl, I await news of the 1867 other ways to do it... :-)
(1866 to go :)
FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.
- Follow-Ups:
- Re: Question on regex substitution using variables...
- From: Ben Bacarisse
- Re: Question on regex substitution using variables...
- References:
- Question on regex substitution using variables...
- From: Ian
- Re: Question on regex substitution using variables...
- From: Ben Bacarisse
- Question on regex substitution using variables...
- Prev by Date: Re: Question on regex substitution using variables...
- Next by Date: Re: Question on regex substitution using variables...
- Previous by thread: Re: Question on regex substitution using variables...
- Next by thread: Re: Question on regex substitution using variables...
- Index(es):