Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex resetting the capture buffer

Reply
Thread Tools

Regex resetting the capture buffer

 
 
Mario D'Alessio
Guest
Posts: n/a
 
      06-21-2007
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



 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      06-22-2007
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
 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      06-22-2007
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.
 
Reply With Quote
 
Mario D'Alessio
Guest
Posts: n/a
 
      06-22-2007
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM
resetting buffer on /dev/dsp Andrew Collier C Programming 3 08-20-2003 01:20 PM
RegisterHiddenField to Capture Enter Resetting Application Matthew Wieder ASP .Net 1 07-22-2003 06:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57