A function to replace substrings based on regex patterns.

PostgreSQL function that replaces text matching a regex pattern with replacement text. Supports backreferences ($1, $2) to insert captured groups. Essential for complex text transformations that exceed LIKE capabilities.

Compatibility

Show:
Database System Support Status Since Version Notes
MySQL ✓ Supported 8.0 REGEXP_REPLACE() added in MySQL 8.0.
PostgreSQL ✓ Supported 9.1
SQL Server ✗ Not Supported SQL Server does not support REGEXP_REPLACE.
Oracle ✓ Supported 12c
SQLite ✗ Not Supported SQLite does not support REGEXP_REPLACE.

Details

This function is useful for text manipulation and data cleaning. However, support for it varies widely across SQL databases, with some lacking native regex replacement capabilities. Perhaps the ones that don't support it have played the 'I used regex to solve my problem; now I have two problems' game and rightly decided against supporting it.

Standard Syntax

SELECT REGEXP_REPLACE(string, pattern, replacement);

Version Support

MySQL: Since 8.0 PostgreSQL: Since 9.1 SQL Server: Not supported Oracle: Since 12c SQLite: Not supported

Per-Database Syntax & Notes

MySQL

REGEXP_REPLACE() added in MySQL 8.0.

SELECT REGEXP_REPLACE(string, pattern, replacement);

PostgreSQL

SELECT REGEXP_REPLACE(string, pattern, replacement);

Oracle

SELECT REGEXP_REPLACE(string, pattern, replacement) FROM dual;