Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Getting an IP out of a string

by the_slycer (Chaplain)
on Aug 16, 2000 at 20:45 UTC ( [id://28141]=perlquestion: print w/replies, xml ) Need Help??

the_slycer has asked for the wisdom of the Perl Monks concerning the following question:

This is probably a newbie question, but..
I have some strings like this:
<li><a href=http://10.127.73.218/foo>foobar</a></li> or <li><a href=http://10.127.73.218/boat>foobar 2000</a></li> etc..
I am relatively new to Perl and regular expressions in general, what I need to do is get rid of everything except the IP address. I can succesfully match the IP using hints from here but am uncertain how to strip all the rest of the string out. The format should end up as: 10.127.73.218, any help from some of you gurus would be great.

Replies are listed 'Best First'.
Re: Getting an IP out of a string
by Cirollo (Friar) on Aug 16, 2000 at 20:47 UTC
RE: Getting an IP out of a string
by merlyn (Sage) on Aug 16, 2000 at 20:47 UTC
RE: Getting an IP out of a string
by knight (Friar) on Aug 16, 2000 at 21:07 UTC
    Don't try to search for the form of the IP address in the string, use the "http:" and URL delimiters in the regex:
    $host = m#http://([^/]+)*#;
    That will also select a hostname if the URL is something like "http://foo.bar.com/foo".

    HTML::Parser is a better solution, but the above would still work if you're forced into parsing non-conformant HTML.
      Problem is it's not always going to be http://ipaddress in front - IE it's not always a link :-)

      Sometimes it will be surrounded by formatting tags, other times by link tags.. basically looking through an HTML file for any IP address. Can do that no problem using hints from node mentioned above, but that returns the whole line - now need to strip anything but the IP :-)

      Sorry if I wasn't clearer to start out with, starting to play with HTML::Parser now..
        The problem is that what is or is not an IP address really depends on the file semantics, not just on what an IP address "looks like." A simple regex match for dotted-quad "IP addresses" in arbitrary text will give you false positives.

        In the HTML source for one site I maintain, for example, such a regex would match the following:
        1.41.1.1
        Is it an IP address? Nope, it's an RCS version number, and blindly assuming it's an IP address would be wrong at best and dangerous at worst.

        That said,
        @ip_addrs = m/{regex to match IP}/g;
        will select only the "IP address" texts from $_ for whatever regex you choose, without the danger of relying on $1.
Re: Getting an IP out of a string
by raflach (Pilgrim) on Aug 16, 2000 at 21:40 UTC
    m/(regex to match IP)/; print $1;
    surround the regex to match the IP with parens, and then use the $1 variable to grab what was matched
      Danger will robinson!

      Never use $1 unless it's in the context of a conditional based on the success of the match. Otherwise, you get the previous success $1, and that's gonna bring you to tears while you're trying to debug it.

      -- Randal L. Schwartz, Perl hacker

        always the wise one.. :) ++merlyn
        Most people will tell you to learn from your mistakes, but I always think it's 100% better if you can learn from someone else's mistakes. This is actually one of the great reasons I visit Perlmonks -- the cooperative learning can't be beat!
        p.s. no offense intended to raflach: I might have made the same mistake.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://28141]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-24 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found