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() 函式更新表中的字串。