Postgres Regex Replace



我有一个这样的表:

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多个诅咒词。

您可以使用单词边界y:

REGEXP_REPLACE(column, 'yCURSEy', '***', 'g')

DB Fiddle上的演示

select col, REGEXP_REPLACE(col, 'yCURSEy', '***', 'g') new_col
from (values ('CURSE IS BAD'), ('DON''T CURSE'), ('IWILLCURSE')) t(col)
col|new_col:---------------|:---------诅咒很糟糕不要诅咒IWILLCURSE|IWILLCCURSE

最新更新