Perl: the pattern matches a string, then prints the following lines / lines

advertisements

I am using Net::Whois::Raw to query a list of domains from a text file and then parse through this to output relevant information for each domain.

It was all going well until I hit Nominet results as the information I require is never on the same line as that which I am pattern matching.

For instance:

Name servers:
ns.mistral.co.uk 195.184.229.229

So what I need to do is pattern match for "Name servers:" and then display the next line or lines but I just can't manage it.

I have read through all of the answers on here but they either don't seem to work in my case or confuse me even further as I am a simple bear.

The code I am using is as follows:

   while ($record = <DOMAINS>) {
     $domaininfo = whois($record);

    if ($domaininfo=~ m/Name servers:(.*?)\n/){
    print "Nameserver: $1\n";
      }

}

I have tried an example of Stackoverflow where

<DOMAINS>;

will take the next line but this didn't work for me and I assume it is because we have already read the contents of this into $domaininfo.

EDIT: Forgot to say thanks! how rude.


So, the $domaininfo string contains your domain?

What you probably need is the m parameter at the end of your regular expression. This treats your string as a multilined string (which is what it is). Then, you can match on the \n character. This works for me:

my $domaininfo =<<DATA;
Name servers:
ns.mistral.co.uk 195.184.229.229
DATA

$domaininfo =~ m/Name servers:\n(\S+)\s+(\S+)/m;
print "Server name = $1\n";
print "IP Address = $2\n";

Now, I can match the \n at the end of the Name servers: line and capture the name and IP address which is on the next line.

This might have to be munged a bit to get it to work in your situation.