in reply to Re: Valid IP?
in thread Valid IP?

An impressive regex, to be sure, but a little oblique, and a little lacking in the functionality department, no offense intended. Here's a quick test that I performed with some numbers that are quite normal:
1.2.3.4 invalid
10.1.1.1 invalid
192.168.1.1 invalid
207.233.0.41 invalid
192.168.20.41 valid
As I mentioned before there are alternate representations of IP addresses apart from N.N.N.N, such as NN.NN, NNN.N or even NNNN. On a binary level, each byte does not have to be separated by a dot. They are for readability only. Those formats are all valid, and work, such as http://3625994804/.
use Socket; sub IsValidIP { return ($_[0] =~ /^[\d\.]*$/) && inet_aton($_[0]); }
Note that the inet_aton function, if given half a chance, will try and convert "www.foo.com" into a number. Since this can take some time, I have included the additional check that the input is purely numerical. When given that kind of input, inet_aton will merely compact the number into the proper internal representation, which is pack type 'N'.