Regex resetting the capture buffer

Discussion in 'Perl Misc' started by Mario D'Alessio, Jun 21, 2007.

  1. Here's an example script:

    my $a = '1 foo';
    my $b = 'foo bar';

    if ( $a =~ /^\d+\s*(\w+)/ ) # line 1
    {
    $b =~ s/bar/$1/; # line 2
    }
    print $b, "\n";

    I am using the capture buffer in line 1 in the regex in line 2. But it
    doesn't
    work. The capture buffer is cleared before the $1 substitution can take
    place.

    I would have thought that Perl would perform the $1 substitution in the line
    2
    before it "recognizes" the substitution command and clears the capture
    buffers.

    Please explain the order in which Perl processes line 1.

    Do I have to "eval" line 2 to get it to work the way that I want without
    having
    to assign $1 to a temp var?

    Thanks.

    Mario
     
    Mario D'Alessio, Jun 21, 2007
    #1
    1. Advertising

  2. Mario D'Alessio

    -berlin.de Guest

    Mario D'Alessio <> wrote in comp.lang.perl.misc:
    > Here's an example script:
    >
    > my $a = '1 foo';
    > my $b = 'foo bar';
    >
    > if ( $a =~ /^\d+\s*(\w+)/ ) # line 1
    > {
    > $b =~ s/bar/$1/; # line 2
    > }
    > print $b, "\n";
    >
    > I am using the capture buffer in line 1 in the regex in line 2. But it
    > doesn't
    > work. The capture buffer is cleared before the $1 substitution can take
    > place.
    >
    > I would have thought that Perl would perform the $1 substitution in the line
    > 2
    > before it "recognizes" the substitution command and clears the capture
    > buffers.
    >
    > Please explain the order in which Perl processes line 1.


    Do you mean "line 2"?

    Perl delays substitution in the replacement string until after the
    match. It *must* do so for constructs like

    s/x(.)y/y$1x/;

    to work. The match must have happened before $1 has its intended value.

    > Do I have to "eval" line 2 to get it to work the way that I want without
    > having
    > to assign $1 to a temp var?


    How would "eval" help? You can use $_ for a temporary variable:

    if ( $a =~ /^\d+\s*(\w+)/ ) # line 1
    {
    $b =~ s/bar/$_/ for $1; # line 2
    }
    print $b, "\n";

    Anno
     
    -berlin.de, Jun 22, 2007
    #2
    1. Advertising

  3. Mario D'Alessio

    Mumia W. Guest

    On 06/21/2007 10:34 AM, Mario D'Alessio wrote:
    > Here's an example script:
    >
    > my $a = '1 foo';
    > my $b = 'foo bar';
    >
    > if ( $a =~ /^\d+\s*(\w+)/ ) # line 1
    > {
    > $b =~ s/bar/$1/; # line 2
    > }
    > print $b, "\n";
    >
    > I am using the capture buffer in line 1 in the regex in line 2. But it
    > doesn't
    > work. The capture buffer is cleared before the $1 substitution can take
    > place.
    >
    > I would have thought that Perl would perform the $1 substitution in the line
    > 2
    > before it "recognizes" the substitution command and clears the capture
    > buffers.
    >
    > Please explain the order in which Perl processes line 1.
    >


    Line 1 is not the issue, and it works as you expect it. Line 2 is the
    problem; when a successful match occurs, the match variables are
    set/reset to new values.

    > Do I have to "eval" line 2 to get it to work the way that I want without
    > having
    > to assign $1 to a temp var?
    >
    > Thanks.
    >
    > Mario
    >
    >
    >


    Using eval() is worse than just assigning to a temporary variable.
     
    Mumia W., Jun 22, 2007
    #3
  4. >> Please explain the order in which Perl processes line 1.
    >
    > Do you mean "line 2"?


    Yes. I just typed a typo.


    Thanks for all the reponses. I now fully understand.

    Mario
     
    Mario D'Alessio, Jun 22, 2007
    #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. Matthew Wieder
    Replies:
    1
    Views:
    377
    Yan-Hong Huang[MSFT]
    Jul 22, 2003
  2. Raja
    Replies:
    12
    Views:
    24,408
    John Harrison
    Jun 21, 2004
  3. Andrew Collier

    resetting buffer on /dev/dsp

    Andrew Collier, Aug 20, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,224
    Kevin Easton
    Aug 20, 2003
  4. Replies:
    2
    Views:
    606
    sergejusz
    Mar 26, 2007
  5. Replies:
    3
    Views:
    768
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page