Difference between two regular expressions

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Difference between two regular expressions


    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 Guest

  2. Similar Questions and Discussions

    1. Regular Expressions :(
      Hello all! I was trying to write down a function to remove all the bbcode tags from a post before showing it (non in the phpbb, in another...
    2. PHP regular expressions
      Hello, I am writing an elearning software with php. I need a script to recognize any inut of the type ab,aabb,aaabbb,aaaabbbb..... And so on....
    3. [PHP] Q on Regular Expressions
      * Thus wrote jsWalter (jsWalter@torres.ws): have you looked at strtotime()? http://php.net/strtotime The Date formats it can find are defined...
    4. [PHP-DEV] PHP regular expressions
      Hello. In regex/utils.h there is a definition for DUPMAX: #ifdef _POSIX2_RE_DUP_MAX #define DUPMAX _POSIX2_RE_DUP_MAX #else #define DUPMAX 255...
    5. Regular Expressions....HELP!
      I'm thinking that I need to use a regular expression to make this work, but I'm not expert at them. What I'd like to do is be able to search a...
  3. #2

    Default Re: Difference between two regular expressions

    Neil Shadrach <neil.shadrach@corryn.com> 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139