use warnings; use strict; my @test_values = qw/ 26 231 232 233 234 254 255 256 025 0000 /; my $rx = qr /^ (?: 2 # first digit is a 2 (?: [6-9] # second and _last_ digit is 6-9 | 5 # or second digit 5 [0-5]? # maybe third and _last_ digit 0-5 | [0-4] # or second digit 0-4 \d? # maybe followed by a final third 0-9 )? | [3-9] # first digit 3-9 \d? # maybe followed by final 0-9 | 1 # first digit 1 (?: \d\d? # maybe followed by 1 or 2 digits )? | 0 # single-digit 0 ) $/x; foreach my $number ( @test_values ) { print "$number:", ($number =~ /$rx/ ? 'true' : 'false'), "\n", ; }