postgresql 正则表达式使用 postgres regexp_replace() 用单引号替换反斜杠和单引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27949530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:45:03  来源:igfitidea点击:

regex to replace backslash and single quote with single quote using postgres regexp_replace()

regexpostgresqlregexp-replace

提问by William Orazi

Just as the title states, I'm not the best w/ regex, so can anyone provide the appropriate regex for the following:

正如标题所说,我不是最好的正则表达式,所以任何人都可以为以下内容提供适当的正则表达式:

UPDATE table SET column = REGEXP_REPLACE(column, {regex}, '''');

Basically, I'd like to replace any instances of backslashes (\) followed by one or moresingle quotes with one single quote.

基本上,我想用一个单引号替换反斜杠 ( \) 后跟一个或多个单引号的任何实例。

So, the string Hello World\'sor Hello World\'''''sshould become Hello World's, but not Hello World\s.

所以,字符串Hello World\'sorHello World\'''''s应该变成Hello World's,而不是Hello World\s

回答by David Faber

This is relatively straightforward. Note that both the backslash character \as well as the single-quote character '(which you escaped in the OP) need to be escaped. The difference is that the backslash has to be escaped in the regex itself, whereas the single quote is escaped in the string literal. Anyway, enough of that digression.

这是相对简单的。请注意,反斜杠字符\和单引号字符'(您在 OP 中转义)都需要转义。不同之处在于反斜杠必须在正则表达式中转义,而单引号在字符串文字中转义。无论如何,离题已经够多了。

UPDATE table SET column = REGEXP_REPLACE(column, '\''+', '''', 'g');

Hope this helps.

希望这可以帮助。

回答by zen

You can try the following:

您可以尝试以下操作:

SELECT REGEXP_REPLACE(column, '\''['']*', '''','g') from table

Edit: of course \''+ is better

编辑:当然 \''+ 更好

SELECT REGEXP_REPLACE(column, '\''+', '''','g') from table