I have the following query:
SELECT
field
FROM
myTable
WHERE
field ~ \'\\\\Qprefix[\\\\E.+\'
It won\'t find values like prefix[fo
This form of regex with a \Q..\E
unquoted substring is only supported by PCRE, which is not available natively in PostgreSQL.
If your program must deal with this syntax in general, the PCRE support can be installed as an extension, as provided here: https://github.com/petere/pgpcre
On the other hand, if it's only that one regex that should be made to work, first
note that the double backslashes in '\\Qprefix[\\E.+'
means literally two backslashes with PostgreSQL 9.1 and above, unless standard_conforming_strings
is explicitly switched to OFF
.
To be insensitive to this setting, literals with the old syntax are expected to be prefixed with E
. This is described in String Constants with C-style Escapes in the doc.
To simply match prefix[foo]
with a PostgreSQL-style regex with the modern syntax, this works:
test=> show standard_conforming_strings ;
standard_conforming_strings
-----------------------------
on
(1 row)
test=> select 'prefix[foo]' ~ 'prefix\[.+';
?column?
----------
t
(1 row)