As of MySQL 8.0 and MariaDB 10 we can use native REGEXP_REPLACE
function. See also See MariaDB docs and PCRE Regular expression enhancements.
-- REGEXP_REPLACE(col, regexp, replace)
SELECT REGEXP_REPLACE("stackoverflow", "(stack)(over)(flow)", '\\2 - \\1 - \\3')
-- returns: over - stack - flow
Match_type arguments (regex modifiers available)
c
– Case sensitive matching.i
– Case insensitive matching.m
– Multiple-line mode. Recognize line terminators within the string. The default behavior is to match line terminators only at the start and end of the string expression.n
– The .
character matches line terminators. The default is for .
matching to stop at the end of a line.u
– Unix-only line endings. Only the newline character is recognized as a line ending by the .
, ^
, and $
match operators.
https://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql
https://www.javatpoint.com/mysql-regexp-replace-function
https://database.guide/how-the-regex_replace-function-works-in-mysql/