Re: Is there any function to test for numeric ips?

From: "Fernando Hevia" <fhevia(at)ip-tel(dot)com(dot)ar>
To: "'Oliveiros C,'" <oliveiros(dot)cristina(at)marktest(dot)pt>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Is there any function to test for numeric ips?
Date: 2009-12-11 16:20:03
Message-ID: D417D556031146289BCD9BA0F96F20DB@iptel.com.ar
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> -----Mensaje original-----
> De: de Oliveiros C,
>
> Dear All,
>
> I have a table with host names and some happen to be numeric IPs.
>
> I would like to be able to filter out the later.
>
> Is there any function pre-defined in the system that can test
> a particular text type value to see if it is a numeric ip?
>
> Something that returns true if applied to '192.168.1.1' but
> false if applied to 'videos.sapo.pt' ?
>
> I considered NOT LIKE '%.%.%.%' but I'm affraid it will
> filter out host names like 'www.google.com.br'
>
> I've realized that, for ex, inet 'x.x.x.x' will fail if the
> input is not a numeric IP, is there any simple and direct way
> to somewhat trap that error and convert it to a false value
> that can be used in a WHERE clause?
>

You could filter IP out with a regular expression:

select hostname as hosts_not_ip
from table
where hostname !~ '^[0-9]\.[0-9]\.[0-9]\.[0-9]$'

Cheers,
Fernando.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Harald Fuchs 2009-12-11 16:23:47 Re: Is there any function to test for numeric ips?
Previous Message Oliveiros C, 2009-12-11 16:05:34 Is there any function to test for numeric ips?