in reply to Re: Leaking Regex Captures
in thread Leaking Regex Captures

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...