Re: Backslash Bug in ARE Class-Shorthand Escape?

From: Joe Conway <mail(at)joeconway(dot)com>
To: David Fetter <david(at)fetter(dot)org>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Backslash Bug in ARE Class-Shorthand Escape?
Date: 2003-12-06 08:26:00
Message-ID: 3FD19298.9060107@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Joe Conway wrote:
> David Fetter wrote:
>> I have a little puzzlement. In the first select, I double the
>> backslash and return true. In the second, I don't and get false.
>> Have I missed something important in the docs?
>
> I don't know if it is clear in the docs anywhere wrt regex, but the
> string literal parser will consume one layer of backslashes on you. So
> in your first case '\\d' is fed into the regex matching function as '\d'
> (string literal parser sees \\ == escape \ == \), and in the second case
> '\d' is fed in as 'd' (string literal parser sees \d == escape d == d).
> The basic rule at work here is you need to double up all backslashes.
>

As a follow-up, there is a statement to this effect in the section on
LIKE, that applies, in part at least, to the regexes as well. See:
http://www.postgresql.org/docs/current/static/functions-matching.html

Specifically, about the 7th paragraph:

"Note that the backslash already has a special meaning in string
literals, so to write a pattern constant that contains a backslash you
must write two backslashes in an SQL statement. Thus, writing a pattern
that actually matches a literal backslash means writing four backslashes
in the statement. You can avoid this by selecting a different escape
character with ESCAPE; then a backslash is not special to LIKE anymore.
(But it is still special to the string literal parser, so you still need
two of them.)"

Part of this should probably be pulled out of the section on LIKE and
into the introduction for pattern matching in general.

Joe

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Bruce Momjian 2003-12-06 10:28:51 Re: Misplaced modifier in Postgresql license
Previous Message Peter Eisentraut 2003-12-06 08:21:35 Re: Backslash Bug in ARE Class-Shorthand Escape?