dbTalk Databases Forums  

[BUGS] Backslash Bug in ARE Class-Shorthand Escape?

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] Backslash Bug in ARE Class-Shorthand Escape? in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
David Fetter
 
Posts: n/a

Default [BUGS] Backslash Bug in ARE Class-Shorthand Escape? - 12-06-2003 , 02:00 AM






Kind people,

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?

Cheers,
D

test=> select version();
version
-------------------------------------------------------------------------------------------------------
PostgreSQL 7.4 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.2.2
20030222 (Red Hat Linux 3.2.2-5)
(1 row)

test=> select ('123' ~ '\\d');
?column?
----------
t
(1 row)

test=> select ('123' ~ '\d');
?column?
----------
f
(1 row)


--
David Fetter david (AT) fetter (DOT) org http://fetter.org/
phone: +1 510 893 6100 cell: +1 415 235 3778

Not believing in force is the same as not believing in gravitation
Leon Trotsky

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Reply With Quote
  #2  
Old   
Peter Eisentraut
 
Posts: n/a

Default Re: [BUGS] Backslash Bug in ARE Class-Shorthand Escape? - 12-06-2003 , 02:23 AM






David Fetter wrote:
Quote:
Kind people,

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?
Yes:

"""
Note: Remember that the backslash (\) already has a special meaning in
PostgreSQL string literals. To write a pattern constant that contains a
backslash, you must write two backslashes in the statement.
"""


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
  #3  
Old   
Joe Conway
 
Posts: n/a

Default Re: [BUGS] Backslash Bug in ARE Class-Shorthand Escape? - 12-06-2003 , 02:27 AM



Joe Conway wrote:
Quote:
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/curre...-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



---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


Reply With Quote
  #4  
Old   
Joe Conway
 
Posts: n/a

Default Re: [BUGS] Backslash Bug in ARE Class-Shorthand Escape? - 12-06-2003 , 02:35 AM



David Fetter wrote:
Quote:
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.

HTH,

Joe



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.