gpt4 book ai didi

regex_replace,仅替换整个单词

转载 作者:行者123 更新时间:2023-11-29 12:49:32 25 4
gpt4 key购买 nike

我的 PostgreSQL 表中有以下行:

kpi_old
---------------------------------
Other operating exp (1200)
Other operating exp (1400)
Other operating exp (1600)
Other operating expenses 0 (1200)
Other operating expenses 0 (1400)
Other operating expenses 0 (1600)

我想替换:

  • Other operational exp => Other operations expenses & taxes
  • 其他运营费用 0 => 其他运营费用和税金

我尝试了以下方法:

SELECT
"kpi_old",
regexp_replace("kpi_old", '(Other operating exp)','Other operating expenses & taxes') as "kpi_new"

问题是,它返回以下内容:

Other operating expenses & taxes (1200)
Other operating expenses & taxes (1400)
Other operating expenses & taxes (1600)
Other operating expenses & taxesenses 0 (1200)
Other operating expenses & taxesenses 0 (1400)
Other operating expenses & taxesenses 0 (1600)

我可以使用哪个正则表达式来替换字符串中的精确匹配?

最佳答案

使用可选组 (enses)?

=# select regexp_replace('Other operating expenses 0 (1600)', '(Other operating exp(enses)?)', 'Other operating expenses & taxes');
regexp_replace
-------------------------------------------
Other operating expenses & taxes 0 (1600)
(1 row)

=# select regexp_replace('Other operating exp (1600)', '(Other operating exp(enses)?)', 'Other operating expenses & taxes');
regexp_replace
-----------------------------------------
Other operating expenses & taxes (1600)

关于regex_replace,仅替换整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57659434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com