469,353 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Question on regex substitution using variables...

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
--
Ian

"Tamahome!!!" - "Miaka!!!"
Feb 2 '06 #1
4 11892
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".
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... :-)

--
Ben.

Feb 2 '06 #2
Ian
On 2006-02-02, Ben Bacarisse <be********@bsb.me.uk> wrote:

[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!

--
Ian

"Tamahome!!!" - "Miaka!!!"
Feb 2 '06 #3
In article <pa****************************@bsb.me.uk>, Ben Bacarisse
<be********@bsb.me.uk> 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
Feb 2 '06 #4
On Thu, 02 Feb 2006 12:26:21 -0800, Jim Gibson wrote:
In article <pa****************************@bsb.me.uk>, Ben Bacarisse
<be********@bsb.me.uk> 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.

Feb 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Reinhold Birkenfeld | last post: by
reply views Thread by Justin | last post: by
8 posts views Thread by Just Me | last post: by
9 posts views Thread by | last post: by
5 posts views Thread by Chris | last post: by
7 posts views Thread by MtiPaulo | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by Chris O | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.