Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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\.]+));

Update

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.


In reply to Re: More complicated regular expression? by TomDLux
in thread More complicated regular expression? by jaldama

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found