What is the equivalent of regexp \Q…\E in postgresql?

一个人想着一个人 提交于 2019-12-11 02:35:14

问题


I have the following query:

SELECT
   field
FROM
   myTable
WHERE
   field ~ '\\Qprefix[\\E.+'

It won't find values like prefix[foo].

How can I replace \Q..\E ?


回答1:


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)


来源:https://stackoverflow.com/questions/21313964/what-is-the-equivalent-of-regexp-q-e-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!