How to use MariaDB's REGEXP_REPLACE?

1.5k views Asked by At

I have read the docs for MariaDB's REGEX_REPLACE but cannot get my query to work. I am storing links in a column, link and want to change the end of the link:

From www.example.com/<code> to www.example.com/#/results/<code> where <code> is some hexidecimal hash, e.g. 55770abb384c06ee00e0c579. What I am trying is:

SELECT REGEX_REPLACE("link", "www\\.example\\.com\\/(.*)", "www\\.example\\.com\\/#\\/results\\/\\1");

The result is:

Showing rows 0 - 0.

1

There are 1 answers

0
jds On BEST ANSWER

I wasn't able to figure out what the first argument was--the documentation says "subject". Turns out it's just the column name. So this works:

UPDATE my_table
SET my_link = REGEXP_REPLACE(
    my_link,
    "http:\\/\\/www\\.example\\.com\\/(.*)",
    "http:\\/\\/www\\.example\\.com\\/#\\/results\\/\\1")
WHERE my_link IS NOT NULL