Replacing Text with Regular Expressions in MySQL
Question:
Can MySQL replace text via regular expressions using a function similar to the REPLACE() function? This functionality would enable searches for and replacements of specific characters or patterns.
Answer:
Yes, MySQL 8.0 and MariaDB offer the REGEXP_REPLACE() function. This function allows for the replacement of text based on a specified regular expression.
Usage:
REGEXP_REPLACE(col, regexp, replace)
Example:
To replace all occurrences of special characters (outside of letters, numbers, and certain punctuation) with an empty string, use the following query:
SELECT REGEXP_REPLACE(filename, '[^a-zA-Z0-9()_ .\-]', '') FROM table;
Note:
The above is the detailed content of Can MySQL Replace Text Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!