my $re =qr/ ^(\d{1,3})$ # Up to 3 digits (?(?{ ($1<256 and $1>=0) }) # If in the correct range .? # An allways match | # Else . # Never going to match after end of string ) /x;