Question on regex substitution using variables...

I

Ian

Hi,

Hopefully a simple question but my brain is hurting...

I want to make a regex substitution, using search and replace
patterns contained in variables. What I want to do is:

$f = "fred.abc";
$f =~ s/(.*)\.abc/$1.def/;
print "$f\n";

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".

Is this possible, and if so, what quoting magic do I need to make
it work???

TIA,
Ian.



#!/usr/bin/perl -w

use strict;

my $f;

$f = "fred.abc";
$f =~ s/(.*)\.abc/$1.def/;

print "$f\n"; # prints "fred.def" (correct)


$f = "joe.abc";

my $to_pattern = "(.*)\\.abc";
my $from_pattern = "\$1.def";

$f =~ s/$to_pattern/$from_pattern/;

print "$f\n"; # prints "$1.def"

# end
 
B

Ben Bacarisse

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".

That is because the second part of the s/// construct is an ordinary
double quoted string. Once you have put a literal $ into a string, it is
not re-interpreted whenever the string is substituted. (What would you
expect

my $a = "X"; my $b = "\$a"; print "$b"

to print?)
Is this possible, and if so, what quoting magic do I need to make it
work???

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;

This being Perl, I await news of the 1867 other ways to do it... :)
 
I

Ian

[snip - understood]
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;

Ta, this gives the effect I'm after. I was contemplating doing the whole
thing in an eval() but this is neater.
This being Perl, I await news of the 1867 other ways to do it... :)

At least!
 
J

Jim Gibson

Ben Bacarisse said:
....
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top