my $ip_mask = parse_ip_mask('131.202.1.[3-4]'); foreach ( 'Connection established to 131.202.1.2', 'Connection established to 131.202.1.3', 'Connection established to 131.202.1.4', 'Connection established to 132.202.1.4', ) { my ($ip) = /(\d+\.\d+\.\d+\.\d+)/; print("Found matching ip $ip.$/") if (ip_check($ip, $ip_mask)); } __END__ output ====== Found matching ip 131.202.1.3. Found matching ip 131.202.1.4.