I have a database with about 500,000 entries. One of the fields, a varchar(255) UTF8 column named file_name
, contains file names.
I want to remove unusual characters from these file names using a pattern similar to [^a-zA-Z0-9()_ .\-]
.
Is there a MySQL function that allows regex-based string replacement? I am looking for something that behaves like the REPLACE() function but utilizes regular expressions.
SELECT REPLACE('example', 'ex', 're');
-- Output: "remample"
/* Does such a function exist? */
SELECT REGEX_REPLACE('Example','/[A-Za-z]/','*');
-- Expected: "*x*mpl*"
I am familiar with REGEXP/RLIKE, which only checks for matches and doesn’t actually perform replacements.
Alternatively, I could execute a query like SELECT id,file_name FROM my_table WHERE file_name RLIKE '[^a-zA-Z0-9()_ .\-]'
from a PHP script and use preg_replace
, but that feels like a clunky solution.