具有匹配表达式的PostgreSQL regexp_replace [英] PostgreSQL regexp_replace with matched expression

查看:683
本文介绍了具有匹配表达式的PostgreSQL regexp_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PostgreSQL regexp_replace函数来转义字符串中的方括号,括号和反斜杠,以便我可以将该字符串用作正则表达式模式本身(在使用此字符串之前,还对该字符串进行了其他操作,但是它们不在此问题的范围内.想法是替换:

I am using PostgreSQL regexp_replace function to escape square brackets, parentheses and backslash in a string so that I could use that string as a regex pattern itself (there are other manipulations done on this string as well before using it, but they are outside the scope of this question. The idea is to replace:

[\[
]\]
(\(
)\)
\\\

[ with \[
] with \]
( with \(
) with \)
\ with \\

有关正则表达式的Postgres文档页面指出以下内容:

替换字符串可以包含\ n,其中n为1到9,以 指示与第n个括号匹配的源子字符串 应该插入模式的子表达式,它可以包含\& 指示与整个模式匹配的子字符串应为 已插入.如果您需要在文字中加上反斜杠,请输入\ 替换文字.

The replacement string can contain \n, where n is 1 through 9, to indicate that the source substring matching the n'th parenthesized subexpression of the pattern should be inserted, and it can contain \& to indicate that the substring matching the entire pattern should be inserted. Write \ if you need to put a literal backslash in the replacement text.

但是regexp_replace('abc [def]', '([\[\]\(\)\\])', E'\\\1', 'g');会生成abc \ def\.

在同一页面上,给出一个示例,该示例使用\\1表示法-所以我尝试了.

Further down on that same page, an example is given, which uses \\1 notation - so I tried that.

但是,regexp_replace('abc [def]', '([\[\]\(\)\\])', E'\\\\1', 'g');生成abc \1def\1.

我猜想这是预期的,但是regexp_replace('abc [def]', '([\[\]\(\)\\])', E'.\\1', 'g');会产生abc .[def.].也就是说,转义可用于标准反斜杠以外的字符.

I would guess this is expected, but regexp_replace('abc [def]', '([\[\]\(\)\\])', E'.\\1', 'g'); produces abc .[def.]. That is, escaping works with characters other than the standard backslash.

目前我不知道如何进行.我该怎么办才能真正给我我想要的替代品?

At this point I don't know how to proceed. What can I do to actually give me the replacement I want?

推荐答案

好的,找到了答案.显然,我需要在替换中将转义符转义两次.另外,在旧版本的postgres(本例中为8.3)上,我需要在搜索模式中使用E -prefix和双转义反斜杠.最终代码如下:

OK, found the answer. Apparently, I need to double-escape the backslash in the replacement. Also, I need to E-prefix and double-escape backslashes in the search pattern on older versions of postgres (8.3 in my case). The final code looks like this:

regexp_replace('abc [def]', E'([\\[\\]\\(\\)\\\\\?\\|_%])', E'\\\\\\1', 'g')

是的,它看起来很可怕,但是有效:)

Yes, it looks horrible, but it works :)

这篇关于具有匹配表达式的PostgreSQL regexp_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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