Question on regex substitution using variables...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian

    Question on regex substitution using variables...

    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!!!"
  • Ben Bacarisse

    #2
    Re: Question on regex substitution using variables...

    On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote:
    [color=blue]
    > 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".[/color]

    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?)
    [color=blue]
    > Is this possible, and if so, what quoting magic do I need to make it
    > work???[/color]

    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_patt ern)/e;

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

    --
    Ben.

    Comment

    • Ian

      #3
      Re: Question on regex substitution using variables...

      On 2006-02-02, Ben Bacarisse <ben.usenet@bsb .me.uk> wrote:

      [snip - understood]
      [color=blue]
      > 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_patt ern)/e;[/color]

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

      At least!

      --
      Ian

      "Tamahome!! !" - "Miaka!!!"

      Comment

      • Jim Gibson

        #4
        Re: Question on regex substitution using variables...

        In article <pan.2006.02.02 .18.23.39.19133 4@bsb.me.uk>, Ben Bacarisse
        <ben.usenet@bsb .me.uk> wrote:
        [color=blue]
        > On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote:
        >[color=green]
        > > 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".[/color][/color]
        ....[color=blue]
        > 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_patt ern)/e;[/color]

        A second e modifier will force a second round of evaluation:

        $f =~ s/$to_pattern/$from_pattern/ee;
        [color=blue]
        >
        > This being Perl, I await news of the 1867 other ways to do it... :-)[/color]

        (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 **
        ----------------------------------------------------------
        Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

        Comment

        • Ben Bacarisse

          #5
          Re: Question on regex substitution using variables...

          On Thu, 02 Feb 2006 12:26:21 -0800, Jim Gibson wrote:
          [color=blue]
          > In article <pan.2006.02.02 .18.23.39.19133 4@bsb.me.uk>, Ben Bacarisse
          > <ben.usenet@bsb .me.uk> wrote:
          >[color=green]
          >> On Thu, 02 Feb 2006 17:06:09 +0000, Ian wrote:
          >>[color=darkred]
          >> > 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".[/color][/color]
          > ...[color=green]
          >> 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_patt ern)/e;[/color]
          >
          > A second e modifier will force a second round of evaluation:
          >
          > $f =~ s/$to_pattern/$from_pattern/ee;[/color]

          Lovely! I did not know that.
          [color=blue][color=green]
          >> This being Perl, I await news of the 1867 other ways to do it... :-)[/color]
          >
          > (1866 to go :)[/color]

          No, that's the one as far as I'm concerned!
          [color=blue]
          > FYI: this newsgroup is defunct. Try comp.lang.perl. misc in the future.[/color]

          Thanks for the heads up.

          --
          Ben.

          Comment

          Working...