Re: order of (escaped) characters in regex range

From: InterRob <rob(dot)marjot(at)gmail(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: Szymon Guz <mabewlun(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: order of (escaped) characters in regex range
Date: 2011-12-13 13:51:15
Message-ID: CA+DVeYDA_7g5EveuaRnee+ZDf5C5vc4FLKZwbutNeBecY7k_qA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks guys, i see what you mean.

I do intend to use the PG escaping, in order to avoid that annoying
warning... Hence, my expression should indeed be:
SELECT regexp_matches('123-A' , E'(3[A-Z\\-\\(\\) ])');

In the above expression i added the parentheses as I whish to match these
as well :))

Thanks!

2011/12/13 David Johnston <polobo(at)yahoo(dot)com>

> On Dec 13, 2011, at 8:09, Szymon Guz <mabewlun(at)gmail(dot)com> wrote:
>
>
>
> On 13 December 2011 14:04, InterRob < <rob(dot)marjot(at)gmail(dot)com>
> rob(dot)marjot(at)gmail(dot)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.
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2011-12-13 14:14:35 Re: order of (escaped) characters in regex range
Previous Message David Johnston 2011-12-13 13:30:51 Re: order of (escaped) characters in regex range