Re: order of (escaped) characters in regex range - Mailing list pgsql-general

From David Johnston
Subject Re: order of (escaped) characters in regex range
Date
Msg-id 2E41E24A-86EB-4EA1-9B71-B6D0C4B07252@yahoo.com
Whole thread Raw
In response to Re: order of (escaped) characters in regex range  (Szymon Guz <mabewlun@gmail.com>)
Responses Re: order of (escaped) characters in regex range  (InterRob <rob.marjot@gmail.com>)
List pgsql-general
On Dec 13, 2011, at 8:09, Szymon Guz <mabewlun@gmail.com> wrote:



On 13 December 2011 14:04, InterRob <rob.marjot@gmail.com> wrote:
Dear List,

I found this interesting:

SELECT regexp_matches('123-A' , E'(3[A-Z\- ])');
ERROR:  invalid regular expression: invalid character range

whereas:
SELECT regexp_matches('123-A' , E'(3[\- A-Z])');
 regexp_matches
----------------
 {3-}
(1 row)

Notice the order of (escaped) characters and ranges in the last bit of the expression.

Am I missing some key concept of the regular expression?

Regards,
Rob

Hi Rob,
try '\\-' instead of '\-'
and it works :)

regards


If you don't intend to use PostgreSQL escapes in your string then omit the leading 'E'.

In a character class the - symbol has special meaning if it appears anywhere but the first character of the group. To avoid that special meaning you have to escape it.  If it appears first it always means a literal -.  The PostgreSQL documentation does not fully describe RegularExpressions but a reference book on them would note this particular behavior.

David J.

pgsql-general by date:

Previous
From: InterRob
Date:
Subject: Re: order of (escaped) characters in regex range
Next
From: InterRob
Date:
Subject: Re: order of (escaped) characters in regex range