首页 > 解决方案 > MariaDB regexp_replace 反向引用未按预期工作

问题描述

我在用户定义的函数中有这个简单的正则表达式

set mm = regexp_replace(in_str, '^M([0-9]+(\.[0-9]+)?).*$', '\\1');

在 MariaDB 10.1 中传入“M56-5”时,我会按预期得到“56”,但在 MariaDB 10.4 中,我收到错误“第 1 行的列 'mm' 的数据被截断”。当我传入“M56.5”时,我在两个版本中都按预期得到了“56.5”。有什么改变吗?

标签: mariadbregexp-replacebackreference

解决方案


I figured it out. I mistakenly was using a single backslash to escape the "." whereas it is required to use 2 backslashes when escaping special characters, one for the MariaDB parser and one for the regex library. https://mariadb.com/kb/en/regular-expressions-overview/#escaping


推荐阅读