Gary's Blog

MySQL REGEXP_REPLACE() function

Notice: Corrently, REGEXP_REPLACE() do not support \s.

Here is an example of using MySQL 8+ regexp_replace():

> select regexp_replace('<div>Hello <b>world</b></div>', '<b>.*?</b>', 'MySQL', 1, 0, 'in') as result;
+------------------------+
| result                 |
+------------------------+
| <div>Hello MySQL</div> |
+------------------------+

Above example correspond to this syntax:

REGEXP_REPLACE(expr, pat, repl[, pos[, occurrence[, match_type]]])

To replace a column value with REGEXP_REPLACE():

> update `name` = regexp_replace(`name`, '<b>.*?</b>', 'MySQL', 1, 0, 'in') where id = 10;

References:

  1. 12.8.2 Regular Expressions
  2. How to do a regular expression replace in MySQL?
Exit mobile version