Re: match an IP address - Mailing list pgsql-general

From hubert depesz lubaczewski
Subject Re: match an IP address
Date
Msg-id 20080923072616.GA26593@depesz.com
Whole thread Raw
In response to match an IP address  (Joao Ferreira gmail <joao.miguel.c.ferreira@gmail.com>)
Responses Re: match an IP address
Re: match an IP address
List pgsql-general
On Mon, Sep 22, 2008 at 05:59:25PM +0100, Joao Ferreira gmail wrote:
> I'm unable to build a LIKE or SIMILAR TO expression for matching and ip
> address
> 192.168.90.3
> 10.3.2.1
> any help please...

use this regular expression:

'^[0-9]{1,3}(.[0-9]{1,3}){3}$'

warning: do not use "like" or "similar to".
proper way to use it:

select * from table where field ~ '^[0-9]{1,3}(.[0-9]{1,3}){3}$';

this regexp is not 100% fault proof - it will happily return rows like:
'300.999.998.7'

but for most of the cases it should be enough. if you need it to match
only ips, and filter out things like '300.999.999.999' - let me know.

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz  /  blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

pgsql-general by date:

Previous
From: Tino Wildenhain
Date:
Subject: Re: match an IP address
Next
From: Peter Eisentraut
Date:
Subject: Re: [ADMIN] 8.3.4 rpms for Opensuse10.3 64bit