in reply to matching between 1 to 8 digits but not more

You need to consider what comes before and after the digits (\D is a non-digit):
use strict; use warnings; while( my $Number = <DATA> ) { chomp $Number; if($Number =~ /(\D|^)\d{1,8}(\D|$)/){ print "$Number is OK\n" } else { print "$Number has more than 8 digits!\n" } } __DATA__ 1234567 12345678 123456789 grunge 1234567 grunge grunge 12345678 grunge grunge 123456789 grunge
Gives:
1234567 is OK 12345678 is OK 123456789 has more than 8 digits! grunge 1234567 grunge is OK grunge 12345678 grunge is OK grunge 123456789 grunge has more than 8 digits!