regexp_replace 替换字符串后面的第一个匹配项,并返回完整字符串

问题描述 投票:0回答:1

我有一个字符串,上面写着“不要替换此文本,而是替换最后一个文本<-- this one".

我想替换字符串后面的第一个匹配项,并返回完整的字符串,如下所示:“不要替换此文本,而是替换最后一个<-- this one

select regexp_replace("dont replace this text, but instead replace the last text <-- this one", "\\b(text)\\b", "ONE")

执行上述操作会将“文本”的两个实例替换为“ONE”,这不是我想要的。

我试过了:

select regexp_replace("dont replace this text, but instead replace the last text <-- this one", "\\b.*(text)\\b", "ONE")

但是这个在比赛前切断了一切,我只剩下“一个”<-- this one"

sql regex hive impala regexp-replace
1个回答
0
投票

如果 Hive 的正则表达式支持负向前瞻,您可以使用此版本:

SELECT REGEXP_REPLACE(
    'dont replace this text, but instead replace the last text',
    '\\btext\\b(?!.*\\btext\\b)',
    'ONE'
)
© www.soinside.com 2019 - 2024. All rights reserved.