Re: Regexp confusion

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Doug Gorley <doug(dot)gorley(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regexp confusion
Date: 2009-10-09 23:37:13
Message-ID: 20091009233713.GI5303@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Doug Gorley escribió:
> Trying to match some numbers, and I'm having some regexp problems.
> I've boiled it down to the following:
>
> /* (1) */ select '3.14' similar to E'^\\d+\\.\\d+$'; -- true
> /* (2) */ select '3.14' similar to E'^\\d+(\\.\\d+)$'; -- true
> /* (3) */ select '3.14' similar to E'^\\d+(\\.\\d+)*$'; -- true
> /* (4) */ select '3.14' similar to E'^\\d+(\\.\\d+)?$'; -- false
> /* (5) */ select '3.14' similar to E'^\\d+(\\.\\d+)+$'; -- true
>
> So, based on (1) and (2), the pattern '\.\d+' occurs once. So why
> does (4) return false? between (3), (4), and (5), it appears as
> though the group is matching multiple times.

I think the confusion is about what SIMILAR TO supports. ? it doesn't.
See here:
http://www.postgresql.org/docs/8.4/static/functions-matching.html#FUNCTIONS-SIMILARTO-REGEXP

You probably want to use ~ instead of SIMILAR TO.

(SIMILAR TO is a weird beast that the SQL committee came up with,
vaguely based on regular expressions.)

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2009-10-09 23:39:14 Re: transaction ID wraparound - should I use 'VACUUM' or 'VACUUM FULL' ?
Previous Message Kelly Jones 2009-10-09 23:07:35 Wiki consisting of rows in a db table