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'.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.