use Regexp::Common qw /net/; if ($givenIP =~/^$RE{net}{IPv4}$/){ # got good IP. }