use Net::Patricia; my $pt = new Net::Patricia; while (<>) { if ( /((?:\d{1,3}\.){3}\d{1,3}\/\d{1,2})/ ) { $pt->add_string($1, 'Inside networks'); } } print "$host: Inside\n" if $pt->match_string($host);