PostgreSQL regexp_replace with matched expression

纵然是瞬间 提交于 2019-12-07 08:47:19

问题


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 documentation page on regular expressions states the following:

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.

However regexp_replace('abc [def]', '([\[\]\(\)\\])', E'\\\1', 'g'); produces abc \ def\.

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

Yet, regexp_replace('abc [def]', '([\[\]\(\)\\])', E'\\\\1', 'g'); produces abc \1def\1.

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?


回答1:


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 :)




回答2:


it's simpliest way

select regexp_replace('abc [def]', '([\[\]\(\)\\])', '\\\1', 'g')


来源:https://stackoverflow.com/questions/12088271/postgresql-regexp-replace-with-matched-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!