It appears that the combination of repetition, alternation and captures can cause the captures to "leak".

Seen on 5.10 and 5.8:
use strict; #use warnings; #printing lots of undefs print "Enter your test strings:\n"; while (<main::DATA>) { chomp; print "\tTesting '$_':\n"; /^(?:(?:(\d+)\s*c\s*)|(?:(\d+)\s*w\s*)|(?:(\d+)\s*r\s*))+/i; print "Capturing \\d+ only: 1='$1', 2='$2', 3='$3'\n"; /^(?:(?:(\d+\s*c)\s*)|(?:(\d+\s*w)\s*)|(?:(\d+\s*r)\s*))+/i; print "Capturing \\d+ plus the letter: 1='$1', 2='$2', 3='$3'\n"; } __DATA__ 1c 2w 2c3w 1w1w 1w2r 2r1c

Note that the only difference in the regexes is the placement of the capture's closing bracket.

The above prints:
Enter your test strings: Testing '1c': Capturing \d+ only: 1='1', 2='', 3='' Capturing \d+ plus the letter: 1='1c', 2='', 3='' Testing '2w': Capturing \d+ only: 1='', 2='2', 3='' Capturing \d+ plus the letter: 1='', 2='2w', 3='' Testing '2c3w': Capturing \d+ only: 1='3', 2='3', 3='' Capturing \d+ plus the letter: 1='2c', 2='3w', 3='' Testing '1w1w': Capturing \d+ only: 1='1', 2='1', 3='' Capturing \d+ plus the letter: 1='', 2='1w', 3='' Testing '1w2r': Capturing \d+ only: 1='2', 2='2', 3='2' Capturing \d+ plus the letter: 1='', 2='1w', 3='2r' Testing '2r1c': Capturing \d+ only: 1='1', 2='', 3='2' Capturing \d+ plus the letter: 1='1c', 2='', 3='2r'

The second regex does what I expect. I can't fathom why the first regex would do what it does, however. I expected it would be the same as the second regex, minus the letters. Instead, after the first repetition, it seems that a match for $2 spills a copy into $1, and a match for $3 spills copies into $2 and $1... but only if the captures contain the same regex pattern (\d+ in this case).

Would I be correct to suspect that this is a bug or mis-optimization of some kind in perl?


The monks can fry your fish, and they can give you some tips and some bait, but you still need to wake up in the morning and climb onto the boat.

In reply to Leaking Regex Captures by SuicideJunkie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.