1

我在 PostgreSQL (9.2) 中尝试以下 regex_replace 语句

 # SELECT regexp_replace('_this._is_a_long_entry._of.nonsense_text', '\._|^_', '$','g');

客观的:

是用'$'替换所有以句点开头的下划线字符。我还替换了锚定在位置 1 的下划线;这是有效的。我想将两场比赛都塞进一个声明中。所有这些都是为了开发一种机制,将我们拥有的一些奇怪的文本推送到 PostreSQL ltree 结构中。

问题:

如何在不影响句号的情况下进行上述第一次替换?

我希望看到结果如下所示:

$this.$is_a_long_entry.$of.nonsense_text

注意:也尝试过显式捕获下划线,但 PG 实现似乎忽略了这一点:

# SELECT regexp_replace('_this.is_long_entry._of.nonsense_text', '\.(_)|^_', '$','g');
4

1 回答 1

2
SELECT regexp_replace('_this._is_a_long_entry._of.nonsense_text'
                     ,'(\.|^)_' -- capture the preceding char (none at start)
                     , '\1$'    -- use the captured char
                     ,'g');     -- globally

这是假设standard_conforming_strings = ON

于 2012-09-26T19:59:55.983 回答