s/$match/$replace/ fails when $replace has backreferences

Discussion in 'Perl Misc' started by anonyusenet@yahoo.com, Sep 6, 2005.

  1. Guest

    I need to store my match and replace strings in variables.

    This fails when my match string uses back references to groupings.

    Here for example I try to replace "foo" with "foobar" using a back
    reference:

    #!/usr/bin/perl
    $content = "this is my foo";
    $match = "(foo)";
    $replace = "\1bar";
    #$replace = "\\1bar"; # this doesn't work either
    #$replace = "$1bar"; # neither does this
    $content =~ s/$match/$replace/;
    print "$content\n"


    Any ideas how to get this to work? I need the match and replace strings
    in variables because I am reading them from file.

    Thanks!
     
    , Sep 6, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > I need to store my match and replace strings in variables.
    >
    > This fails when my match string uses back references to groupings.
    >
    > Here for example I try to replace "foo" with "foobar" using a back
    > reference:
    >
    > #!/usr/bin/perl
    > $content = "this is my foo";
    > $match = "(foo)";
    > $replace = "\1bar";
    > #$replace = "\\1bar"; # this doesn't work either
    > #$replace = "$1bar"; # neither does this
    > $content =~ s/$match/$replace/;
    > print "$content\n"
    >
    >
    > Any ideas how to get this to work? I need the match and replace strings
    > in variables because I am reading them from file.
    >



    One possibility:

    # perldoc -q expand and consider a hash instead of a double eval
    # Also see perlre: \<digit> vs. $<digit> discussion

    $replace = '"$1bar"'; # or $replace = '$1 . "bar"'
    $replace =~ s/$match/$replace/ee;
    die $@ if $@;

    htm,
    --
    Charles DeRykus
     
    , Sep 6, 2005
    #2
    1. Advertising

  3. <> wrote:
    >
    > I need to store my match and replace strings in variables.
    >
    > This fails when my match string uses back references to groupings.
    >
    > Here for example I try to replace "foo" with "foobar" using a back
    > reference:
    >
    > #!/usr/bin/perl



    Please have the courtesy of posting programs that work with both:

    use strict;
    use warnings;


    > $content = "this is my foo";
    > $match = "(foo)";
    > $replace = "\1bar";


    $replace has a 4-character string in it now since the double
    quotes ate the backslash.


    > #$replace = "\\1bar"; # this doesn't work either



    You should use the backslash-digit form only *within* a
    regular expression.

    The replacement part of an s/// is not a regular expression,
    so you should use the dollar-digit form of backref.


    > #$replace = "$1bar"; # neither does this



    The double quotes interpolate the value of $1 _now_ rather than
    further down in the code after $1 has been set.


    > $content =~ s/$match/$replace/;
    > print "$content\n"
    >
    >
    > Any ideas how to get this to work?



    ------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $content = "this is my foo";
    my $match = "(foo)";
    my $replace = '"$1bar"'; # double-quotish for 2nd "e"val
    #my $replace = '$1 . "bar"'; # concatenation for 2nd "e"val
    $content =~ s/$match/$replace/ee;
    print "$content\n"
    ------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 6, 2005
    #3
  4. Brian McCauley, Sep 12, 2005
    #4
    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. Mark Fletcher
    Replies:
    1
    Views:
    506
    Mark Fletcher
    May 19, 2004
  2. Chris Nolte
    Replies:
    9
    Views:
    4,313
    Jeff Schwab
    May 25, 2004
  3. dhek bhun kho

    java.util.regex: Backreferences?

    dhek bhun kho, Jul 9, 2003, in forum: Java
    Replies:
    2
    Views:
    810
    dhek bhun kho
    Jul 9, 2003
  4. Amy Dillavou

    backreferences

    Amy Dillavou, Sep 28, 2005, in forum: Python
    Replies:
    4
    Views:
    456
    Peter
    Sep 28, 2005
  5. John Gordon
    Replies:
    13
    Views:
    513
    Ian Kelly
    Dec 20, 2011
Loading...

Share This Page