my $test = "2123"; if($test=~/^\d{4}$/) { print "0$test\n"; } else { print "$test\n"; }