use strict; #use warnings; #printing lots of undefs print "Enter your test strings:\n"; while () { 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 #### 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'