... while (<$fh>) { chomp; my $ip=""; if (m/(?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/){ $ip = $+{matched_ip}; } print $ip,$/; } ...