有人可以解释 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)
第二个参数是正则表达式。 $
标记字符串的结尾。
第三个参数用于标志。 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/