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 match) # gives the 'right' results # - regex 1 (same as 3) # if excecuted before regex 2, screws the results from regex 2 as well! # Aaaargh! #################### print "Enter your test strings:\n"; while () { 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