I have been trying to understand how SuicideJunkie's code causes the results it does, and I am getting lost.
jwkrahn - do you mean that $1 etc... are not being reset if they do not match? So as SuicideJunkie asked - how come they seem to inherit the value of the 'next' match? i.e. $2 = $3 (but only if $3 matches first...)?

I also found that the \s* part of the regex is causing some of the problem - i.e. see regex 2 below - in isolation it works as expected),
but then i moved around the order of the regexes and came back the same problem with the 'fixed' regex 2 now 'inheriting' the faulty results from regex 1.

use strict; #################### # in summary: # - regex 3 (same as originally posted more or less) # gives the 'wrong' results # - regex 2 (eq regex 2 but wothout the \s* part before the letter ma +tch) # gives the 'right' results # - regex 1 (same as 3) # if excecuted before regex 2, screws the results from reg +ex 2 as well! # Aaaargh! #################### print "Enter your test strings:\n"; while (<main::DATA>) { chomp; print "\n***Testing '$_':\n"; ########### # NB: regex 1 eq regex 3 # includes \s* # if removed 2 and 3 appear to work correctly ########### m/ ^(?: (?: (\d+) \s* ## the offending bit a\s* ) | (?: (\d+) \s*b\s* ) | (?: (\d+) \s*c\s* ) )+/ixg; print "Capturing \\d+ only (with \\s\*): 1='$1', 2='$2', 3='$3'\n +"; ############### # regex 2 # no spaces # gives expected output ONLY if regex 1 is removed ############### m/ ^(?: (?: (\d+) a\s* ) | (?: (\d+) b\s* ) | (?: (\d+) c\s* ) )+/ixg; print "Capturing \\d+ only: 1='$1', 2='$2', 3='$3'\n"; ############## # regex 3 # gives correct output only if only preceeded by regex 2 ############## m/ ^(?: (?: (\d+) \s*a\s* ) | (?: (\d+) \s*b\s* ) | (?: (\d+) \s*c\s* ) )+/ixg; print "Capturing \\d+ only (with \\s\*): 1='$1', 2='$2', 3='$3'\n +"; /^(?:(?:(\d+\s*a)\s*)|(?:(\d+\s*b)\s*)|(?:(\d+\s*c)\s*))+/ix; print "Capturing \\d+ plus the letter: 1='$1', 2='$2', 3='$3'\n"; } __DATA__ 1a 2b 3c 2a3b 1b1b 1b2c 2c1a 1a2c 1c2a 1a2b3c 3c2b1a

This behaviour really confuses me! And sorry to SuidiceJunkie again for jumping on his node!!

Just a something something...

In reply to Re^2: Leaking Regex Captures by BioLion
in thread 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.