my $ip4 = "([0-9]{1,3}\.){3}[0-9]{1,3}"; my $ip6 = "([a-f0-9]{4}\:)+?"; my $prefix = "/\d\d"; #### my $pattern = "^($ip4|$ip6)($prefix)?"; #### open (FILE, "<$some_file") or die "Error: $!\n"; while () { next if !~ m[$pattern]; print "Hey, I found \$1: $1 \$2: $2 \$3: $3\n"; } close FILE; #### ^(([0-9]{1,3}\.){3}[0-9]{1,3}|([a-f0-9]{4}\:)+?)(/\d\d)? #### "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"