Difference between two regular expressions

Discussion in 'Perl Misc' started by Neil Shadrach, Sep 11, 2003.

  1. 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[]
    Neil Shadrach, Sep 11, 2003
    #1
    1. Advertising

  2. Neil Shadrach

    Anno Siegel Guest

    Neil Shadrach <> 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
    Anno Siegel, Sep 11, 2003
    #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. Jay Douglas
    Replies:
    0
    Views:
    591
    Jay Douglas
    Aug 15, 2003
  2. jakk
    Replies:
    4
    Views:
    12,024
  3. Replies:
    3
    Views:
    488
    Wibble
    May 28, 2005
  4. Replies:
    4
    Views:
    550
    J├╝rgen Exner
    Apr 12, 2005
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page