PostgreSQL中的正则表达式\Q ... \E等效于什么? [英] What is the equivalent of regexp \Q...\E in postgresql?

查看:111
本文介绍了PostgreSQL中的正则表达式\Q ... \E等效于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

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

它找不到像 prefix [foo] 这样的值。

如何替换 \Q..\E

推荐答案

这种形式的正则表达式与仅仅支持 \Q..\E 未引用的子字符串。 PCRE ,这在PostgreSQL中是不可用的。

This form of regex with a \Q..\E unquoted substring is only supported by PCRE, which is not available natively in PostgreSQL.

如果您的程序通常必须使用这种语法,则可以将PCRE支持安装为扩展,如此处提供: https://github.com/petere/pgpcre

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

另一方面,如果仅应使用一个正则表达式,则首先
请注意'\\Qprefix [the]中的双反斜杠。 \\E。+'表示PostgreSQL 9.1的两个反斜杠以上,除非将 standard_conforming_strings 显式切换为 OFF
为了对此设置不敏感,使用旧语法的文字应加 E 作为前缀。在字符串常量中进行了描述文档中的C样式转义符

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.

要将前缀[foo] 与PostgreSQL进行简单匹配样式的正则表达式具有现代语法,可以正常工作:

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)

这篇关于PostgreSQL中的正则表达式\Q ... \E等效于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆