SQL正则表达式量词操作数在postgres 9.5.16上无效,在9.6.12上有效

时间:2019-03-05 12:48:58

标签: postgresql

我有一个SQL查询:

select
  concat('PL', (regexp_matches( mt_line.info_to_owner, '(?<=~31)(.*?)(?=~)'))[1]) AS "sender",
  (regexp_matches( mt_line.info_to_owner, '(?<=38)(.*?)(?=$)'))[1] AS "receiver"
from b.mt_line AS mt_line;

当我在本地数据库PostgreSQL版本9.6.12中执行它时,它工作正常,但在测试数据库-PostgreSQL 9.5.16版本中不起作用。

我在此出现以下错误:

[2201B] ERROR: invalid regular expression: quantifier operand invalid

如果我将此查询更改为:

select
  concat('PL', (regexp_matches( mt_line.info_to_owner, '.*'))[1]) AS "sender",
  (regexp_matches( mt_line.info_to_owner, '.*'))[1] AS "receiver"
from b.mt_line AS mt_line;

然后在两种版本的postgres上都能正常工作。

1 个答案:

答案 0 :(得分:1)

lookbehind是在postgres 9.6中引入的,请参见: https://www.postgresql.org/docs/9.6/functions-matching.html

选中Table 9-17. Regular Expression Constraints

(?<=re) positive lookbehind matches at any point where a substring matching re ends (AREs only)
(?<!re) negative lookbehind matches at any point where no substring matching re ends (AREs only)

Postgres 9.5没有以下选项:https://www.postgresql.org/docs/9.5/functions-matching.html 校验 Table 9-15. Regular Expression Constraints