$pattern = '[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]'; /(?:(${pattern})\.){3}($pattern)/; # "$1.$2.$3.$4" = IP if matched #### sub get_octets { my @octets = split(/\./, shift); return unless @octets == 4; foreach (@octets) { $_ += 0; # Perhaps unnecessary, but forces the "octet" to be numeric or zero return unless $_ >= 0 && $_ <= 255; } return @octets; }