regexp_replace()

Replaces the substring that is the first match to the POSIX regular expression pattern, or all such matches if the g flag is used; see Section 9.7.3.

Signature

regexp_replace ( string text, pattern text, replacement text [, flags text ] ) → text
regexp_replace ( string text, pattern text, replacement text, start integer [, N integer [, flags text ] ] ) → text

Examples

SELECT regexp_replace('Thomas', '.[mN]a.', 'M');
ThM
SELECT regexp_replace('Thomas', '.', 'X', 3, 2);
ThoXas
SELECT regexp_replace(string=>'hello world', pattern=>'l', replacement=>'XX', start=>1, "N"=>2);
helXXo world

Run against PostgreSQL 18.6 on 2026-09-13. This is the output we got, not the documentation’s.

In other engines

PostgreSQLregexp_replace
MySQLREGEXP_REPLACE(x, p, r) on MySQL 8.0+
SQL Serverno direct equivalent
OracleREGEXP_REPLACE(x, p, r)
SQLiteno direct equivalent

SQL Server has no regular expressions in T-SQL before 2025, and SQLite has none without an extension. This is usually the function that decides a piece of logic belongs in the application rather than the query.

Want to run this against your own PostgreSQL server from an iPhone, iPad or Mac? These are ours — every connection goes straight from the device to your database, with no account and no server of ours in between.

Signature and description adapted from the PostgreSQL manual, used under the PostgreSQL License. PostgreSQL is a registered trademark of the PostgreSQL Community Association of Canada. This page is not affiliated with the PostgreSQL Global Development Group.