use strict; use warnings; use 5.010; my $localhost = qr/(?!10\.20\.11\.99\s)/; while () { next if /^\s*$/; next if /^\s*#.*$/; if (/^$localhost([a-fA-F0-9:\.]+)(\s+[a-zA-Z0-9\.-]+)+\s*$/) { say "IP: $1; HOST: $2"; } else { chomp; say "No match for '$_'"; } } __DATA__ 127.0.0.1 localhost 10.20.11.99 kvm-test-v06.example.com kvm-test-v06 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters # Spurious records 1.2.3.4 litter trash kvm-test-v06.example.com garbage 1.2.3.5 kvm-test-v06 more garbage 1.2.3.6 litter kvm-test-v06 1.2.4.7 kvm-test-v06.example.com kvm-test-v06