Reference to groups in a MySQL regex?

Solution 1:

(Old question, but top search result)

For MySQL 8:

SELECT REGEXP_REPLACE('stackoverflow','(.{5})(.*)','$2$1');
-- "overflowstack"

You can create capture groups with (), and you can refer to them using $1, $2, etc.

For MariaDB, capturing is done in REGEXP_REPLACE with \\1, \\2, etc. respectively.

Solution 2:

You can't, there is no way to reference regex capturing groups in MySql.