Re: match an IP address

From: Steve Atkins <steve(at)blighty(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: match an IP address
Date: 2008-09-23 15:37:53
Message-ID: E61916EF-A2A5-41F8-85A7-0C0828205C54@blighty.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Sep 23, 2008, at 12:26 AM, hubert depesz lubaczewski wrote:

> 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'

It'll also return rows like 3L33T0o7 - which normally I'd not be too
bothered about, but on a forum that's a fairly typical username. :)

Gotta escape that period:

^[0-9]{1,3}(?:\.[0-9]{1,3}){3}$

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Phoenix Kiula 2008-09-23 15:38:53 Re: match an IP address
Previous Message Tino Wildenhain 2008-09-23 13:06:24 Re: match an IP address