Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: More complicated regular expression?

by TomDLux (Vicar)
on Jan 26, 2012 at 20:01 UTC ( #950210=note: print w/replies, xml ) Need Help??

in reply to More complicated regular expression?

Is the question about doing DNS lookups in Perl? In that case use an appropriate module, such as Net::DNS::Lookup, or one of the others you find by querying CPAN for 'DNS Lookup'

If your actual concern is achieving a greater understanding of regular expressions, try:

  • Match the constant part of the string, and capture the ip4 that follows ... I'm actually capturing the longest possible string of digits and dots, but you can generally rely on DNS servers to send values that make sense as IP numbers.
  • my ($ipnum) = ($msg =~ m{has address ([\d\.]+)});
  • Break the regex processing into simple components:
  • LINE: for my $line ( split "\n", $dns_data ) { next LINE unless index $line, 'has address'; my ($ipnum) = ($msg =~ m{([\d\.]+));


You wrote:

    if ($found_addr =~ m/address(.*)\d/){ print "$1\n"; }

    But it's cutting off the last digit of the address for some reason.

Let's go back to basics and discuss this block: IF (the stuff between parentheses is true)THEN DO {the stuff between curly braces}.

the stuff between parentheses is applying a regular expression to the value stored in the variable $found_addr. The regular expression looks for the sequence of characters: 'a', 'd', 'd', 'r', 'e', 's', 's', followed by a wildcard sequence which is captured, followed by a digit which is not captured.

From our knowledge of what was being returned, we know you are capturing a space character, ' ', followed by some digits, a dot, some digits, .... Since the captured sequence is followed by a non-captured digit, the last digit will not be captured. When you look at your printout, you'll notice the IP address is one character to the right, because of the captured space.

If you want to capture the digit, put it within the capturing parentheses. If you want to exlude the space, explicitly specify it outside the parentheses.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950210]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2018-05-21 15:22 GMT
Find Nodes?
    Voting Booth?