Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: Validate Ip address Regexp

by Laurent_R (Monsignor)
on Nov 28, 2015 at 22:53 UTC ( #1148785=note: print w/ replies, xml ) Need Help??

in reply to Re: Validate Ip address Regexp
in thread Validate Ip address Regexp

Regular expression is the wrong approach IMHO.
Well, quite possibly, but the OP wanted to practice regular expressions (which is why I gave a pure regex solution) and, besides, using split is in fact using regular expressions.

But I agree that checking for numbers in the 0-255 range with a pure regex is somewhat unwieldy. An easier way might be something along these lines:

print "Valid IP\n" if 4 == grep { /^\d+$/ and $_ < 256 } split /\./, $ +ip;
(although this is still not entirely satisfactory, since this would validate something like "23.45.aa3.234.244"", so a bit more effort might be needed).

Note that Perl 6's regexes allow a code assertion to be inserted within a regex, leading for example to something like this:

my regex octet {(\d ** 1..3) <?{0 <= $0 <= 255 }> } my regex ip {^ <octet> ** 4 % '.' $}

Comment on Re^2: Validate Ip address Regexp
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2015-11-29 07:02 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (748 votes), past polls