Thread: Backslash Bug in ARE Class-Shorthand Escape?
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@fetter.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
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. HTH, Joe
David Fetter wrote: > 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. """
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