MariaDB REGEXP_REPLACE() Function

In MariaDB, the REGEXP_REPLACE() function replaces a substring matching the specified regular expression with the new content.

MariaDB REGEXP_REPLACE() Syntax

Here is the syntax for the MariaDB REGEXP_REPLACE() function:

REGEXP_REPLACE(str, regexp, replacement)

Parameters

str

Required. a string.

regexp

Required. regular expression.

replacement

Required. The string to replace.

Note that at the time of writing, MariaDB’s version accepts fewer arguments than MySQL’s REGEXP_REPLACE(). MySQL’s REGEXP_REPLACE() allow you to provide parameters for where to start the search, which matches to search for, and to refine regular expressions with match types.

If you provide the wrong number of parameters, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'REGEXP_REPLACE'.

Return value

The MariaDB REGEXP_REPLACE() function uses replacement to replace the content matched regexp in str and returns the result.

If str, replacement or regexp is NULL, REGEXP_REPLACE() will return NULL.

MariaDB REGEXP_REPLACE() Examples

SELECT REGEXP_REPLACE('123 abc 456 def', '\\s+', '-');

Output:

+------------------------------------------------+
| REGEXP_REPLACE('123 abc 456 def', '\\s+', '-') |
+------------------------------------------------+
| 123-abc-456-def                                |
+------------------------------------------------+

In this example, we replaced all spaces with -.

Conclusion

In MariaDB, the REGEXP_REPLACE() function replaces t a substring matching the specified regular expression with the new content.