my $regex = '(2[0-4]|1?[0-9])?[0-9]'; while (<>) { chomp; if ($_ =~ /^$regex$/) { print "$_ matched\n"; } else { print "$_ did not match\n"; } }