use Regexp::Common qw(net); while (<>) { my ($keeper) = $_ =~ / ($RE{net}{IPv4}) # Capture the IP , # comma /x; printf "%s\n", $keeper if $keeper; }