Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

IPv4 regex

by ryan (Pilgrim)
on Feb 18, 2001 at 17:05 UTC ( #59241=perlquestion: print w/ replies, xml ) Need Help??
ryan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've needed to validate user input to ensure it is a validly formatted IPv4 address.

The one I came up with before I looked around was:
/(^(\d{1,3}\.){3})\d{1,3}$/
The double brackets are to ensure the ^ stands for 1 triplet, not all three of them such as  (^\d{1,3}\.){3} which could never occur

Is there anything failsafe yet more efficient than this?

After hunting around I discovered suprisingly that many mail programs and even the LRP favour using this regex:
/^\d+\.\d+\.\d+\.\d+$/
Which will fail on things like 1111.0.0.0

As a side note, I'm sure there is a way to compare two variables to the same regex in one neat expression rather than
if ($var1 =~ /$regex/&&$var2 =~ /$regex/)
I guess for an array a foreach can be used, but just for 2 variables only is there a way?

Thanks folks.

Comment on IPv4 regex
Select or Download Code
Re: IPv4 regex correction
by ryan (Pilgrim) on Feb 18, 2001 at 17:44 UTC
    Correction sorry,

    The double brackets are because I wasn't paying attention when I cut and pasted it and proceeded to comment on it.

    My regex should read:
    /^(\d{1,3}\.){3}\d{1,3}$/

    *yawn* :)

Re: IPv4 regex
by boo_radley (Parson) on Feb 18, 2001 at 17:54 UTC
    here's something off the top of my head.
    don't rush to trade compactness for readability or usefullness,btw.
    my $ip ="1.133.123.123"; if (IP_valid ($ip)) {print "$ip ok\n"} else {print "$ip bad\n"} my $ip =""; if (IP_valid ($ip)) {print "$ip ok\n"} else {print "$ip bad\n"} my $ip ="1asdf1.133.123.123"; if (IP_valid ($ip)) {print "$ip ok\n"} else {print "$ip bad\n"} my $ip ="12341.133."; if (IP_valid ($ip)) {print "$ip ok\n"} else {print "$ip bad\n"} my $ip ="not at all valid"; if (IP_valid ($ip)) {print "$ip ok\n"} else {print "$ip bad\n"} sub IP_valid { my $ip = shift; $ip =~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; foreach ($1,$2,$3,$4){ if ($_ <256 && $_ >0) {next;} return 0; } return 1; }
      Yes, I normally wrongly assume compactness in favour of readability. I'll learn one day :)

      I forgot to check the numbers were <=255, mainly because unpack "N" that I use mostly assumes they are zero if above 255, which is fine for my application

Re: IPv4 regex (Net::IPv4Addr)
by ybiC (Prior) on Feb 18, 2001 at 18:21 UTC
    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")
      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")
Re: IPv4 regex
by Tuna (Friar) on Feb 18, 2001 at 19:43 UTC
    Not sure if it matters to your program, but within the range of "numerically valid" IP addresses that everyone's suggestions capture, there are some ranges that are reserved by certain protocols. For instance, the subnet 224.0.0.0, is reserved for Cisco multicast group addresses. There are other examples, as well, such as reserved private subnets 172.0.0.0 and 192.0.0.0. These subnets are not publicly routable. There are other examples, as well.
    Just thought that I would kick in my $.02, in case it mattered
    Update:As merlyn has adeptly pointed out, in an indirect fashion, Tuna should NOT post before getting caffeine into his system. Sorry for the misinformation.
      It's not 172.* and 192.*. According to rfc1918:
      The Internet Assigned Numbers Authority (IANA) has reserved the following three blocks of the IP address space for private internet +s: 10.0.0.0 - 10.255.255.255 (10/8 prefix) 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)

      -- Randal L. Schwartz, Perl hacker

Re: IPv4 regex
by BlueLines (Hermit) on Feb 19, 2001 at 04:13 UTC
    There's a good discussion of this here.

    BlueLines

    Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.
Re: IPv4 regex
by tadman (Prior) on Feb 19, 2001 at 05:08 UTC
    It's funny how people go to the ends of the Earth and back again to do something as simple as validate an IP address, and they don't even handle all possible valid addresses.
    use Socket; sub IsValidIP { return $_[0] =~ /^[\d\.]*$/ && inet_aton($_[0]); }
    This code uses only standard Perl packages.

    Again, N.N.N.N is not the only valid IP address format. Try visiting http://3625994804/ and you will see.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-12-20 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls