Re: Question on regex substitution using variables...
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Thu, 02 Feb 2006 22:49:06 +0000
On Thu, 02 Feb 2006 12:26:21 -0800, Jim Gibson wrote:
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;
Lovely! I did not know that.
This being Perl, I await news of the 1867 other ways to do it... :-)
(1866 to go :)
No, that's the one as far as I'm concerned!
FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
Thanks for the heads up.
--
Ben.
.
- References:
- Question on regex substitution using variables...
- From: Ian
- Re: Question on regex substitution using variables...
- From: Ben Bacarisse
- Re: Question on regex substitution using variables...
- From: Jim Gibson
- Question on regex substitution using variables...
- Prev by Date: Re: Question on regex substitution using variables...
- Next by Date: adding strings, not concatenating them
- Previous by thread: Re: Question on regex substitution using variables...
- Next by thread: adding strings, not concatenating them
- Index(es):