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"; } } #### A: , First case' #### checking '123456789second & third case' A: 1234, 56789second & third case B: 1234, 56789second & third case #### checking '123456789second & third case' A: 1234, 56789second & third case B: 123456789, second & third case #### $ perl xxxyyyzzz.pl checking 'First case' checking '123456789second & third case' A: 1234, 56789second & third case B: 1234, 56789second & third case