Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: IPv4 regex (Net::IPv4Addr)

by ybiC (Prior)
on Feb 18, 2001 at 18:21 UTC ( [id://59249]=note: print w/replies, xml ) Need Help??


in reply to IPv4 regex

Have you considered the Net::IPv4Addr CPAN module?   I've yet to use it myself, but it looks like it might fit your bill.

From README:
Net::IPv4Addr provides functions for parsing IPv4 addresses both in traditional address/netmask format and in the new CIDR format.   There are also methods for calculating the network and broadcast address and also to see check if a given address is in a specific network.

And from POD:

ipv4_checkip if ($ip = ipv4_checkip($str) ) { # Do something } Return the IPv4 address in the string or undef if the input doesn't co +ntains a valid IPv4 address.
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

Replies are listed 'Best First'.
Re: IPv4 regex (Net::IPv4Addr)
by ryan (Pilgrim) on Feb 18, 2001 at 18:38 UTC
    I got the module and this is how they do it:
    my $ip_rgx = "\\d+\\.\\d+\\.\\d+\\.\\d+"; sub ipv4_chkip($) { my ($ip) = $_[0] =~ /($ip_rgx)/o; return undef unless $ip; # Check that bytes are in range for (split /\./, $ip ) { return undef if $_ < 0 or $_ > 255; } return $ip; }
    Perfect, I'm not going to use the module for my application though because I wanted it to run off standard packaged modules so some totally inept computer users could make use of the script without my assitance. The code however may be making an appearance -- Thanks.

      Heh. I just wanted to note that the test for $_ < 0 is a waste since nothing matching \d+ can be negative.

              - tye (but my friends call me "Tye")
        perl -e 'use integer; print 3000000000 + 1'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found