2023年11月18日
MySQL
经实测,至少在MySQL 8.0.35版本中 regexp_replace 函数不支持反向引用分组。
在MySQL官方文档中也没有找到关于捕获分组的内容:
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace
MariaDB
MariaDB 可以使用\N反向引用捕获的分组, N是1到9的数字:
Backreferences to the subexpressions in the form \N, where N is a number from 1 to 9:
SELECT REGEXP_REPLACE('James Bond','^(.*) (.*)$','\\2, \\1') AS reorder_name;
-> Bond, James
参考MariaDB官方文档…