PostgreSQL 替换字符串
-
使用 PostgreSQL 中的
replace()函数替换字符串 -
在 PostgreSQL 中使用
regexp_replace()函数替换字符串 -
更多 PostgreSQL
replace()函数示例
本教程讨论如何使用 PostgreSQL replace() 函数替换字符串。
使用 PostgreSQL 中的 replace() 函数替换字符串
PostgreSQL replace() 函数具有以下参数,这些参数都是文本类型:
replace (string text, from text, to text)
string 参数是执行 replace() 函数的源文本。from 参数是 string 参数的子字符串,表示要更改的部分。
to 参数表示 from 参数要更改为的 string 参数的子字符串。
例如,让我们使用 replace() 函数将单词 Catfish 更改为 Turtle,如下所示:
SELECT replace ('Catfish', 'Catfish', 'Turtle');
结果是:
replace
---------
Turtle
让我们将 Catfish 中的子字符串 Cat 替换为 Jelly,如下所示:
SELECT replace('Catfish', 'Cat', 'Jelly');
结果是:
replace
-----------
Jellyfish
此函数还可用于替换字符串中的特殊字符。例如,用逗号替换空格,如下所示:
SELECT replace('A B C D E', ' ', ',');
结果是:
replace
-----------
A,B,C,D,E
假设我们有一个随机字符的文本,并且我们想用 b 替换每个出现的字符 a,无论字符 a 是大写还是小写。如果我们运行这个命令,如下所示:
SELECT replace('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() 函数,如下所示:
SELECT regexp_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() 函数,如下所示:
UPDATE demo SET text = 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() 函数可以实现这一点,如下所示:
UPDATE demo SET text = 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() 函数来实现,如下所示:
UPDATE demo SET text = 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
CREATE TABLE demo (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
text TEXT NOT NULL
);
--insert first set of records
INSERT INTO demo (text)
VALUES
('Red'),
('Green'),
('Blue'),
('Red'),
('Red');
--insert second set of records
INSERT INTO demo (text)
VALUES
('g-r-e-e-n'),
('1-23--4'),
('one-and-two'),
('---n---2---'),
('-----');
--insert third and final set of records
INSERT INTO demo (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
UPDATE demo SET text = replace(text, 'Red', 'Yellow');
UPDATE demo SET text = replace(text, '-', '_');
UPDATE demo SET text = replace(text, 'red', 'yellow');
--view all the changes
SELECT * FROM demo;
在本教程中,我们讨论了如何使用 PostgreSQL replace() 和 regexp_replace 函数替换字符串,以及如何使用 replace() 函数更新表中的字符串。