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

Re: IPv4 regex

by boo_radley (Parson)
on Feb 18, 2001 at 17:54 UTC ( [id://59246]=note: print w/replies, xml ) Need Help??


in reply to IPv4 regex

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; }

Replies are listed 'Best First'.
Re: IPv4 regex
by ryan (Pilgrim) on Feb 18, 2001 at 18:01 UTC
    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^2: IPv4 regex
by flakmagnet (Initiate) on Jul 22, 2016 at 17:32 UTC
    I ran into undefined variables while iterating through a multidimensional hash containing all sort of server info using that function. I silenced them with a simple modification (checking if the parts of the IP is "defined"):
    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 ( defined $_ && $_ <256 && $_ >0) {next;} return 0; } return 1; }
    Sure, that could have been added earlier but that if test was already checking multiple factors so it was easier to add. Thanks for this simple write-up! Just what I was looking for. Wow, 15 years later.

      Please also see Regexp::Common and Regexp::Common::net.

      c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; VECTOR: for my $ar_vector ( [ '1.133.123.123', 1 ], [ '1.2.3.4', 1 ], [ '11.22.33.44', 1 ], [ '0.0.0.0', 1 ], [ '255.255.255.255', 1 ], [ '', '' ], [ '1asdf1.133.123.123', '' ], [ '12341.133.', '' ], [ '0.0.0.256', '' ], [ 'not at all valid', '' ], [ '1.2.3.4.5', '' ], [ '1111.2.3.1111', '' ], ) { my ($ip, $valid) = @$ar_vector; ;; my $status = $valid ? 'valid' : 'INVALID'; is IP_valid($ip), $valid, qq{'$ip' $status}; } ;; done_testing; ;; ;; use Regexp::Common qw(net); ;; sub IP_valid { my ($ip) = @_; return $ip =~ m{ \A $RE{net}{IPv4} \z }xms; } " ok 1 - '1.133.123.123' valid ok 2 - '1.2.3.4' valid ok 3 - '11.22.33.44' valid ok 4 - '0.0.0.0' valid ok 5 - '255.255.255.255' valid ok 6 - '' INVALID ok 7 - '1asdf1.133.123.123' INVALID ok 8 - '12341.133.' INVALID ok 9 - '0.0.0.256' INVALID ok 10 - 'not at all valid' INVALID ok 11 - '1.2.3.4.5' INVALID ok 12 - '1111.2.3.1111' INVALID 1..12 ok 13 - no warnings 1..13
      Note that, by design,  $RE{net}{IPv4} is not "anchored", so by itself it will match, e.g.:
      c:\@Work\Perl\monks>perl -wMstrict -le "use Regexp::Common qw(net); ;; print qq{matches IP of '$1'} if '99999.1.2.9999' =~ m{ ($RE{net}{IPv4}) }xms; " matches IP of '99.1.2.99'
      which may or may not be what you want. The  \A \z anchors in the example code above address this (quite intentional) feature.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found