my @tests = ( 'First case', '123456789second & third case', ); for my $t (@tests) { print "\nchecking '$t'\n"; if ($t=~/(\d{4,}?)(.*)/) { print "A: $1, $2\n"; } if ($t=~/(\d{4,}?)(.*?)$/) { print "B: $1, $2\n"; } }