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:
(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).print "Valid IP\n" if 4 == grep { /^\d+$/ and $_ < 256 } split /\./, $ +ip;
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 % '.' $}
|
|---|