Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Difference between two regular expressions

Reply
Thread Tools

Difference between two regular expressions

 
 
Neil Shadrach
Guest
Posts: n/a
 
      09-11-2003

Why do the two 'print' lines behave differently in the following?

#!/usr/bin/perl -w

use strict;
use warnings;

$/.=$/;

while(<DATA>)
{
print "\nA:1[$1]\nA:2[$2]" if /bindings: (?.+ => .+)\n?\s*)(?.+ => .+\n?\s*))/;
print "\nB:1[$1]\nB:2[$2]" if /bindings: (?.+ => .+)\n?\s*){2}/;
}

__DATA__
community: public
enterprise: 9.9.9.9.9.9.999.9.9.9.9
agent addr: 999.99.99.999
agent name: ab.cd.ef.gh
generic ID: 9
specific ID: 999
uptime: 9:99:99
bindings: 9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached maximum size
9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
###
!/tmp/demo.pl

A:1[9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached maximum size]
A:2[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage

]Use of uninitialized value in concatenation (.) at /tmp/demo.pl line 11, <DATA> chunk 1.

B:1[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage]
B:2[]

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-11-2003
Neil Shadrach <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Why do the two 'print' lines behave differently in the following?
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
> $/.=$/;
>
> while(<DATA>)
> {
> print "\nA:1[$1]\nA:2[$2]" if /bindings: (?.+ => .+)\n?\s*)(?.+
> => .+\n?\s*))/;
> print "\nB:1[$1]\nB:2[$2]" if /bindings: (?.+ => .+)\n?\s*){2}/;
> }
>
> __DATA__
> community: public
> enterprise: 9.9.9.9.9.9.999.9.9.9.9
> agent addr: 999.99.99.999
> agent name: ab.cd.ef.gh
> generic ID: 9
> specific ID: 999
> uptime: 9:99:99
> bindings: 9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached
> maximum size
> 9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
> ###
> !/tmp/demo.pl
>
> A:1[9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached maximum size]
> A:2[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
>
> ]Use of uninitialized value in concatenation (.) at /tmp/demo.pl line
> 11, <DATA> chunk 1.
>
> B:1[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage]
> B:2[]


The first patten contains two pairs of capturing parentheses, the second
contains only one. A regex sets only as many $<n> variables as it
captures.

Your problem is obfuscated by a lot of accidential circumstances,
like multiline matching with a non-standard $/, and more. Reduce
it to the simplest terms that still exhibit the problem:

$_ = 'tuttut';

print "$1, $2\n" if /(tut)(tut)/;
print "$1, $2\n" if /(tut){2}/;

Try this technique before posting -- you'll either solve your problem
yourself, or you'll be able to present a much more appealing question.

Anno
 
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
Pulling out data between <TD> tags using regular expressions tdmailbox@yahoo.com Java 3 05-27-2005 11:01 PM
Pulling out data between <TD> tags using regular expressions tdmailbox@yahoo.com Perl Misc 20 05-27-2005 04:44 AM
C# regular expressions, HTML & removal of text between <script> tags john.w.blair@gmail.com Perl Misc 4 04-12-2005 01:07 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments