Question on regex substitution using variables...

Discussion in 'Perl' started by Ian, Feb 2, 2006.

  1. Ian

    Ian Guest

    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!!!"
    Ian, Feb 2, 2006
    #1
    1. Advertising

  2. 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.
    Ben Bacarisse, Feb 2, 2006
    #2
    1. Advertising

  3. Ian

    Ian Guest

    On 2006-02-02, Ben Bacarisse <> 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!!!"
    Ian, Feb 2, 2006
    #3
  4. Ian

    Jim Gibson Guest

    In article <>, Ben Bacarisse
    <> 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
    Jim Gibson, Feb 2, 2006
    #4
  5. On Thu, 02 Feb 2006 12:26:21 -0800, Jim Gibson wrote:

    > In article <>, Ben Bacarisse
    > <> 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.
    Ben Bacarisse, Feb 2, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    753
    Reedick, Andrew
    Jul 1, 2008
  2. Andreas Schwarz

    String substitution without RegEx

    Andreas Schwarz, Jul 22, 2003, in forum: Ruby
    Replies:
    9
    Views:
    196
    Andreas Schwarz
    Jul 23, 2003
  3. Scott Pack

    Variable substitution in a regex

    Scott Pack, May 18, 2005, in forum: Ruby
    Replies:
    2
    Views:
    148
    Nikolai Weibull
    May 18, 2005
  4. globalrev

    how to do this regex substitution?

    globalrev, May 9, 2008, in forum: Ruby
    Replies:
    2
    Views:
    131
    Peter Jones
    May 10, 2008
  5. GarfGarf

    substitution using variables

    GarfGarf, Nov 16, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    118
    GarfGarf
    Nov 17, 2005
Loading...

Share This Page