Re: data type for ip addresses
Posted in 2009
On Fri, Jun 19, 2009 at 16:14, Carsten Haese <carsten.haese@gmail.com>wrote:
> Ian Michael Gumby wrote:
> > IPV4 = "xxx.xxx.xxx.xxx" Where you may or may not have leading zeros.
> > And the values of patter are >= '000' and <='255'.
>
> Of course, but that's not a regular expression. That's exactly my point:
> regular expressions are rarely the right tool for anything, and they're
> definitely not the right tool for determining whether a string
> represents a valid IPv4 address.
>
That depends on what level of validation you want to do. For IPv4 (only),
you can use a simple Perl regex to validate it pretty accurately - for
syntax:
$data ~= m/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}[ ]*$/;
That's imperfect in that it lets through 256.256.256.256, which is not
valid; a more sophisticated expression can be written to limit the
individual components to the range 0..255; you can decide to get antsy about
leading zeroes on terms, or not, as desired.
If you mean validating that the address exists somewhere on the network and
you can access it, then that is certainly not possible with just a regex -
since regexes do not connect to machines over the internet.
> > What do you think your code is going to look like when you have to say
> >
> > SELECT ...
> > FROM foo
> > WHERE first = 127
> > AND second = 0
> > AND third = 0
> > AND fourth = 1> >
> > Much less readable than '127.0.0.1'
>
> True, but if I were to define a custom data type for an IPv4 address,
> I'd define casts to and from CHAR, so that I could write
>
> SELECT ... FROM foo where ip_addr == "127.0.0.1"::IPv4>
> instead.
Yes, you can do more if you define a custom type - complete with casts etc.
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2008.0513 -- http://dbi.perl.org/
"Blessed are we who can laugh at ourselves, for we shall never cease to be
amused."
NB: Please do not use this email for correspondence.
I don't necessarily read it every week, even.
Rodney Dangerfield<http://www.brainyquote.com/quotes/authors/r/rodney_dangerfield.html>
- "What a dog I got, his favorite bone is in my arm."