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

前端 未结 1 1414
旧巷少年郎
旧巷少年郎 2021-01-22 00:31

I have the following query:

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

It won\'t find values like prefix[fo

相关标签:
1条回答
  • 2021-01-22 00:43

    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)
    
    0 讨论(0)
提交回复
热议问题