Skip to main content

REGEXP_REPLACE_ONE

描述

对字符串 STR 进行正则匹配, 将命中 pattern 的部分使用 repl 来进行替换,仅替换第一个匹配项。

  • 字符集匹配需要使用 Unicode 标准字符类型。例如,匹配中文请使用 \p{Han}

语法

REGEXP_REPLACE_ONE(<str>, <pattern>, <repl>)

参数

参数描述
<str>需要进行正则匹配的列。
<pattern>目标模式。
<repl>用于替换匹配模式的字符串。

返回值

替换之后的结果,类型是 Varchar

举例

mysql> SELECT regexp_replace_one('a b c', ' ', '-');
+-----------------------------------+
| regexp_replace_one('a b c', ' ', '-') |
+-----------------------------------+
| a-b c |
+-----------------------------------+

mysql> SELECT regexp_replace_one('a b b', '(b)', '<\\1>');
+----------------------------------------+
| regexp_replace_one('a b b', '(b)', '<\1>') |
+----------------------------------------+
| a <b> b |
+----------------------------------------+

mysql> select regexp_replace_one('这是一段中文This is a passage in English 1234567', '\\p{Han}', '123');
+------------------------------------------------------------------------------------------------+
| regexp_replace_one('这是一段中文This is a passage in English 1234567', '\p{Han}', '123') |
+------------------------------------------------------------------------------------------------+
| 123是一段中文This is a passage in English 1234567 |
+------------------------------------------------------------------------------------------------+