sql - regexp_matches() 返回 $ 的两个匹配项(字符串结尾)

标签 sql regex postgresql

有人可以解释 regexp_matches() 的这种奇怪行为吗?在 PostgreSQL 9.2.4 中(在 9.1.9 中结果相同):

db=# SELECT regexp_matches('test string', '$') AS end_of_string;
 end_of_string
---------------
 {""}
(1 row)

db=# SELECT regexp_matches('test string', '$', 'g') AS end_of_string;
 end_of_string
---------------
 {""}
 {""}
(2 rows)

-> SQLfiddle demo.

第二个参数是正则表达式。 $ 标记字符串的结尾。
第三个参数用于标志。 g 代表“全局”,意味着函数不会在第一次匹配时停止。

该函数似乎用 g 标志报告字符串的结尾两次,但每个定义只能存在一次。它打破了我的查询。 :(
我错过了什么吗?


对于任何可能的字符串,我需要我的查询在末尾再返回 一个 行。我希望这个查询能够完成这项工作,但它添加了行:

SELECT (regexp_matches('test & foo/bar', '(&|/|$)', 'ig'))[1] AS delim

我知道如何手动添加一行,但我想让函数来处理它。

最佳答案

看起来这是 PostgreSQL 中的一个错误。我确认它已在 9.3.8 中修复。查看发行说明,我在以下位置看到了可能的引用资料:

9.3.4

  • Allow regular-expression operators to be terminated early by query cancel requests (Tom Lane)

    This prevents scenarios wherein a pathological regular expression could lock up a server process uninterruptably for a long time.

9.3.6

  • Fix incorrect search for shortest-first regular expression matches (Tom Lane)

    Matching would often fail when the number of allowed iterations is limited by a ? quantifier or a bound expression.

感谢 Erwin 将其缩小到 9.3.x。

关于sql - regexp_matches() 返回 $ 的两个匹配项(字符串结尾),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189095/

相关文章:

ruby-on-rails - Rails 4 自定义 404 导致 Heroku 上的 postgresql 连接失败

sql - SSIS/SQL Server - 处理各种货币类型符号

sql - 如何在单个查询中的直接 SQL 中合并双移动平均线?

regex - 如何使用 RegEx 在 Accumulo 上过滤扫描

python - Pandas - 通过不规则字符串模式的计算创建新列

postgresql - PGSQL JSONB 中带有部分路径的深度 JSON 查询?

未使用的 Postgresql 索引

PHP mysqli 不插入数据库

sql - Couchbase N1QL-内联正则表达式捕获组替换?

regex - 无法在 Swift 中进行正则表达式匹配