postgresql 替换字符串
本篇文章讨论如何使用 postgresql replace() 函数替换字符串。
使用 postgresql 中的 replace() 函数替换字符串
postgresql replace() 函数具有以下参数,这些参数都是文本类型:
replace (string text, from text, to text)
string 参数是执行 replace() 函数的源文本。from 参数是 string 参数的子字符串,表示要更改的部分。
to 参数表示 from 参数要更改为的 string 参数的子字符串。
例如,让我们使用 replace() 函数将单词 catfish 更改为 turtle,如下所示:
selectreplace('catfish','catfish','turtle');
结果是:
replace
---------
turtle
让我们将 catfish 中的子字符串 cat 替换为 jelly,如下所示:
selectreplace('catfish','cat','jelly');
结果是:
replace
-----------
jellyfish
此函数还可用于替换字符串中的特殊字符。例如,用逗号替换空格,如下所示:
selectreplace('a b c d e',' ',',');
结果是:
replace
-----------
a,b,c,d,e
假设我们有一个随机字符的文本,并且我们想用 b 替换每个出现的字符 a,无论字符 a 是大写还是小写。如果我们运行这个命令,如下所示:
selectreplace('agadava','a','b');
结果是:
replace
---------
bgadbva
上面显示的结果不满足将所有出现的大写和小写 a 替换为 b 的要求,因为仅替换了小写 a 的出现。因此,我们必须引入 postgresql regexp_replace() 函数。
在 postgresql 中使用 regexp_replace() 函数替换字符串
postgresql regexp_replace() 函数具有以下参数,这些参数都是文本类型:
regexp_replace (string text, pattern text, replacement text [,flags text])
string 参数是执行替换功能的源字符串。pattern 参数表示在字符串参数的子字符串可以被替换之前必须匹配的正则表达式。
replacement 参数表示我们将字符串参数的子字符串更改为的文本。flags 参数表示可用于更改 regexp_replace() 函数的行为的文本。
在前面的示例中,我们需要将出现的所有大写和小写的 a 更改为 b,我们可以使用 regexp_replace() 函数,如下所示:
selectregexp_replace('agadava','[a | a] ','b','g');
结果是:
regexp_replace
----------------
bgbdbvb
随着正则表达式的引入,所有出现的大写和小写 a 都根据需要替换。g 标志确保替换所有出现的位置,而不仅仅是第一次出现的 a。
这是关于的 postgresql 文档。
更多 postgresql replace() 函数示例
假设我们有一个表,其中有一个名为 text 的列,由单个单词组成,如下所示:
| id | text |
|---|---|
| 1 | red |
| 2 | green |
| 3 | blue |
| 4 | red |
| 5 | red |
我们想用 yellow 替换每个出现的单词 red。我们可以使用 replace() 函数,如下所示:
updatedemosettext=replace(text,'red','yellow');
结果是:
id | text
---- --------
1 | yellow
2 | green
3 | blue
4 | yellow
5 | yellow
假设在同一张表中,我们在 text 字段中有带有特殊字符的单词,如下所示:
| id | text |
|---|---|
| 6 | g-r-e-e-n |
| 7 | 1-23–4 |
| 8 | one-and-two |
| 9 | —n—2— |
| 10 | —– |
我们希望将所有出现的连字符 (-) 替换为下划线 (_)。replace() 函数可以实现这一点,如下所示:
updatedemosettext=replace(text,'-','_');
结果是:
id | text
---- -------------
6 | g_r_e_e_n
7 | 1_23__4
8 | one_and_two
9 | ___n___2___
10 | _____
如果我们向表中插入更多记录,使得 text 字段包含句子而不是单个单词,如下所示:
| id | text |
|---|---|
| 11 | she bought a red bag |
| 12 | green is a good color |
| 13 | the sky is blue |
| 14 | the color of the shirt is red |
| 15 | they plan to go with blue or red balloons |
我们想用 yellow 替换单词 red。这可以使用 replace() 函数来实现,如下所示:
updatedemosettext=replace(text,'red','yellow');
结果是:
id | text
---- ----------------------------------------------
11 | she bought a yellow bag
12 | green is a good color
13 | the sky is blue
14 | the color of the shirt is yellow
15 | they plan to go with blue or yellow balloons
接下来,以下是要运行的命令:
--create statement
createtabledemo(idintegerprimarykeygeneratedalwaysasidentity,texttextnotnull);--insert first set of records
insertintodemo(text)values('red'),('green'),('blue'),('red'),('red');--insert second set of records
insertintodemo(text)values('g-r-e-e-n'),('1-23--4'),('one-and-two'),('---n---2---'),('-----');--insert third and final set of records
insertintodemo(text)values('she bought a red bag'),('green is a good color'),('the sky is blue'),('the color of the shirt is red'),('they plan to go with blue or red balloons');--update statements that include the replace function
updatedemosettext=replace(text,'red','yellow');updatedemosettext=replace(text,'-','_');updatedemosettext=replace(text,'red','yellow');--view all the changes
select*fromdemo;
在本文中,我们讨论了如何使用 postgresql replace() 和 regexp_replace 函数替换字符串,以及如何使用 replace() 函数更新表中的字符串。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在一个 postgresql 查询中使用多个 with 语句
发布时间:2023/03/20 浏览次数:337 分类:postgresql
-
在本教程中,我们将学习如何使用多个 with 语句在 postgresql 中使用两个临时表执行查询。
发布时间:2023/03/20 浏览次数:185 分类:postgresql
-
本文介绍如何在 ubuntu 上找到 postgresql 数据库的配置文件。
发布时间:2023/03/20 浏览次数:409 分类:数据库
-
本文解释了如何直接从终端/命令行或 psql shell 运行 sql 文件。为此,你需要指定主机名、端口、用户名和数据库名称。
发布时间:2023/03/20 浏览次数:89 分类:postgresql
-
本文展示了如何列出 postgresql 上的活动连接。
在 postgresql 中使用循环
发布时间:2023/03/20 浏览次数:966 分类:postgresql
-
在 pl/sql 中,你可能需要在 postgres 中使用循环。我们可以使用 for 和 while 语句来创建循环。
发布时间:2023/03/20 浏览次数:141 分类:postgresql
-
本文介绍如何在 postgresql 中仅使用单个查询来重命名列以及更改其类型。
发布时间:2023/03/20 浏览次数:233 分类:postgresql
-
本文介绍如何在 postgresql 中使用 select 方法连接列。
发布时间:2023/03/20 浏览次数:281 分类:postgresql
-
本文展示了如何使用 case 语句并给出了 postgresql 中的示例。

