PostgreSQL regexp_replace with matched expression

不打扰是莪最后的温柔 提交于 2019-12-05 14:44:56

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

it's simpliest way

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