【问题标题】:Postgres Regex Replace [duplicate]Postgres正则表达式替换[重复]
【发布时间】:2020-05-04 12:51:30
【问题描述】:

我有一张这样的桌子:

Column
-------------
CURSE IS BAD
DON'T CURSE
IWILLCURSE

我想要这样的输出:

Column
-------
*** IS BAD
DON'T ***
IWILLCURSE 

我只想在匹配全字时替换。

我尝试了以下操作:REGEXP_REPLACE(column, '( |CURSE)', '***', 'g') 但它给出的输出如下:

Column
-------
******IS BAD
DON'T ***
IWILLCURSE 

我想在其中处理多个诅咒词。另一种选择是CASE 语句,但我想替换 50 多个脏话。

【问题讨论】:

  • REGEXP_REPLACE(column, '\yCURSE\y', '***', 'g')

标签: sql regex postgresql select


【解决方案1】:

你可以使用字边界\y:

REGEXP_REPLACE(column, '\yCURSE\y', '***', 'g')

Demo on DB Fiddle

select col, REGEXP_REPLACE(col, '\yCURSE\y', '***', 'g') new_col
from (values ('CURSE IS BAD'), ('DON''T CURSE'), ('IWILLCURSE')) t(col)
上校 | new_col :----------- | :--------- 诅咒是坏的 | *** 不好 不要诅咒 |不 *** 诅咒 |我会诅咒

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多