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
$ match operators.