Re: POSIX RE starting with a (

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Nick Barr <nicky(at)chuckie(dot)co(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: POSIX RE starting with a (
Date: 2004-08-11 15:07:00
Message-ID: 1092236819.11679.1152.camel@linda
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 2004-08-11 at 14:39, Nick Barr wrote:
> Hi,
>
> Just noticed something funny with the POSIX Regular expressions and
> wondered if it is a bug, or whether I am doing something wrong. I am
> using the POSIX RE in a function that is used in several places so would
> have to put some nasty if statement in there for this side case if there
> is not work around.
>
> ======================================================================
> intranet=# select track_id, track_name from ms_track where track_name
> like '(%';
> track_id | track_name
> ----------+---------------------------------
> 1294 | (I Can''t Get No) Satisfaction
> 1340 | (Hidden Track)
> 1503 | (Nice Dream)
> 1942 | (I) Get Lost
> (4 rows)
>
> intranet=# select track_name from ms_track where track_name ~ '^\(';
> ERROR: invalid regular expression: parentheses () not balanced
> intranet=# select track_name from ms_track where track_name ~ '^(';
> ERROR: invalid regular expression: parentheses () not balanced
> intranet=# select track_name from ms_track where track_name ~ '^\(';
> ERROR: invalid regular expression: parentheses () not balanced
> ======================================================================
>
> Now I have tried a similar query using the PHP POSIX Regular Expressions
> and it accepts the sequence '^\(' and matches correctly. Is this a
> "feature" of PG that cannot be worked around easily?

You need to escape the backslash:

select track_name from ms_track where track_name ~ '^\\
(';
--
Oliver Elphick olly(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA 92C8 39E7 280E 3631 3F0E 1EC0 5664 7A2F A543 10EA
========================================
"Be still before the LORD and wait patiently for him;
do not fret when men succeed in their ways, when they
carry out their wicked schemes."
Psalms 37:7

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nick Barr 2004-08-11 15:11:17 Re: POSIX RE starting with a (
Previous Message Tom Lane 2004-08-11 15:03:07 Re: POSIX RE starting with a (