How to use backreferences in a variable for a regular expression

Discussion in 'Perl' started by Mark Fletcher, May 18, 2004.

  1. I have tried everything I can think of and cannot solve this. Any help appreciated.

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

    my ($s, $pat, $rpl);

    $s = "ABCDEF abc000 RSTUV";
    $pat = '(.*abc)...(.*)';
    $rpl = '${1}777${2}';

    # Works...
    $_ = $s;
    s/(.*abc)...(.*)/${1}777${2}/;
    print "$_\n";

    # Does not work...
    $_ = $s;
    s/$pat/$rpl/;
    print "$_\n";

    OUTPUT:
    ABCDEF abc777 RSTUV
    ${1}777${2}
     
    Mark Fletcher, May 18, 2004
    #1
    1. Advertising

  2. s/$pat/eval qq("$rpl")/e;
     
    Mark Fletcher, May 19, 2004
    #2
    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. Chris Nolte
    Replies:
    9
    Views:
    4,283
    Jeff Schwab
    May 25, 2004
  2. VSK
    Replies:
    2
    Views:
    2,307
  3. Replies:
    5
    Views:
    464
    Steve Holden
    Aug 2, 2007
  4. Marshall Dudley

    regular expression backreferences

    Marshall Dudley, Feb 15, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    129
    Eric Schwartz
    Feb 15, 2005
  5. Tony
    Replies:
    2
    Views:
    127
    Tad McClellan
    Apr 21, 2005
Loading...

Share This Page