You have to understand that the numerical variables retain their values from the last successful match and '1c' is matched by the first capturing parentheses, and '2w', '2c3w', and '1w1w' are captured by the second capturing parentheses, and '1w2r' and '2r1c' are captured by the third capturing parentheses so the values returned by the other capturing parentheses are not valid and/or undefined. To get valid results only use the contents of the capturing parentheses that actually matched:
use strict; use warnings; print "Enter your test strings:\n"; while ( <DATA> ) { chomp; print "\tTesting '$_':\n"; /^(?:(?:(\d+)\s*c\s*)|(?:(\d+)\s*w\s*)|(?:(\d+)\s*r\s*))+/i and pr +int "Capturing \\d+ only: '$+'\n"; /^(?:(?:(\d+\s*c)\s*)|(?:(\d+\s*w)\s*)|(?:(\d+\s*r)\s*))+/i an +d print "Capturing \\d+ plus the letter: '$+'\n"; } __DATA__ 1c 2w 2c3w 1w1w 1w2r 2r1c
In reply to Re: Leaking Regex Captures
by jwkrahn
in thread Leaking Regex Captures
by SuicideJunkie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |