$ perle ' my @possibles = qw{ 012212316000140 01221231600014 2212316000140 1221231600014 221231600014 1231600014 }; my %re_map = ( 10 => qr{^(\d{10})$}, 12 => qr{^\d{2}(\d{10})$}, 13 => qr{^(?:(?:18|19|20|21|22)(\d{10})\d|\d{3}(\d{10}))$}, 14 => qr{^\d{4}(\d{10})$}, 15 => qr{^\d{4}(\d{10})\d$}, ); for (@possibles) { my $re = $re_map{+length}; /$re/ and say $1 // $2; } ' 1231600014 1231600014 1231600014 1231600014 1231600014 1231600014