603

I have a table with ~500k rows; varchar(255) UTF8 column filename contains a file name;

I'm trying to strip out various strange characters out of the filename - thought I'd use a character class: [^a-zA-Z0-9()_ .\-]

Now, is there a function in MySQL that lets you replace through a regular expression? I'm looking for a similar functionality to REPLACE() function - simplified example follows:

SELECT REPLACE('stackowerflow', 'ower', 'over');

Output: "stackoverflow"

/* does something like this exist? */
SELECT X_REG_REPLACE('Stackoverflow','/[A-Zf]/','-'); 

Output: "-tackover-low"

I know about REGEXP/RLIKE, but those only check if there is a match, not what the match is.

(I could do a "SELECT pkey_id,filename FROM foo WHERE filename RLIKE '[^a-zA-Z0-9()_ .\-]'" from a PHP script, do a preg_replace and then "UPDATE foo ... WHERE pkey_id=...", but that looks like a last-resort slow & ugly hack)

Community
  • 1
  • 1
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • 9
    It's a feature request since 2007: http://bugs.mysql.com/bug.php?id=27389. If you really want this feature, log in and click "Affects me" button. Hopefully it will get enough votes. – Tomas Mar 07 '14 at 17:19
  • 6
    @Tomas: I have done that...in 2009, when I was looking around for it. Since there has been zero progress on it - apparently it's not such an important feature. (btw Postgres has it: http://stackoverflow.com/questions/11722995/regular-expression-find-and-replace-in-postgres ) – Piskvor left the building Mar 09 '14 at 16:45
  • 2
    Related, simpler, version of this question: http://stackoverflow.com/questions/6942973/mysql-how-to-remove-all-non-alpha-numeric-characters-from-a-string – Kzqai Mar 12 '14 at 21:30
  • 2
    I've created `regexp_split` (function + procedure) & `regexp_replace`, which are implemented with `REGEXP` operator. For simple lookups, it will do the trick. You may find it [here](https://github.com/almadomundo/mysql-regexp) - so, this is the way with MySQL stored code, no UDF. If you'll find some bugs, which are not covered by known limitations - feel free to open the issue. – Alma Do Jun 05 '14 at 08:51
  • IMO, selecting, using PHP, and then updating mysql is not such a bad "hack". I do prefer using MySQL native methods when possible - supposedly it's faster. But I have a system that uses a lot of PHP and it does hundreds of thousands of such replacements in a few seconds (the system I built is designed for exactly this kind of work). – Buttle Butkus Oct 13 '16 at 01:53
  • 1
    Found this library from another SO thread: https://github.com/mysqludf/lib_mysqludf_preg works perfectly. – Kyle Nov 27 '17 at 22:14

13 Answers13

181

If you are using MariaDB or MySQL 8.0, they have a function

REGEXP_REPLACE(col, regexp, replace)

See MariaDB docs and PCRE Regular expression enhancements

Note that you can use regexp grouping as well (I found that very useful):

SELECT REGEXP_REPLACE("stackoverflow", "(stack)(over)(flow)", '\\2 - \\1 - \\3')

returns

over - stack - flow
Mike Kormendy
  • 3,389
  • 2
  • 24
  • 21
Benvorth
  • 7,416
  • 8
  • 49
  • 70
  • 13
    this is from mariadb 10 – Nick Oct 07 '14 at 17:02
  • 1
    Benni--thanks for pointing this out, but I'm a little confused on the actual implementation... mind chiming in on my question over here? http://stackoverflow.com/questions/27498929/what-is-the-correct-syntax-for-a-regex-find-and-replace-using-regexp-replace- cc @Piskvor – Jeff Widman Dec 16 '14 at 07:29
  • 11
    For the next time I need it, here's syntax for changing a whole column: `UPDATE table SET Name = REGEXP_REPLACE(Name, "-2$", "\\1")` This removes -2 from abcxyz-2 from a whole column at once. – Josiah Aug 11 '16 at 12:01
  • 35
    Changing an entire platform is hardly a realistic solution. – David Baucum Nov 29 '17 at 22:15
  • 5
    @DavidBaucum MariaDB is a drop-in replacement for MySQL. So it is no "change of platform" but more like choosing a different airline for the same trip – Benvorth Nov 30 '17 at 06:15
  • 4
    @Benvorth [MySQL 8.0 supports it too](https://stackoverflow.com/a/49925597/5070879). – Lukasz Szozda Apr 19 '18 at 16:10
  • What if I want to replace a entire column at once and change s:[1-9] to s:5 for example? I think this is a bit confusing or maybe it's just me... this doesn't work `UPDATE table SET file_source = REGEXP_REPLACE("file_source", "s:[1-9]", "s:12");` – Freedo Aug 04 '19 at 04:54
  • @Benvorth if you've already deployed to production, its more like choosing a different airline for the same trip _mid flight_ – Ulad Kasach Dec 06 '19 at 23:15
181

MySQL 8.0+:

You can use the native REGEXP_REPLACE function.

Older versions:

You can use a user-defined function (UDF) like mysql-udf-regexp.

Community
  • 1
  • 1
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
  • 4
    REGEXP_REPLACE as a User Defined Function? Looks promising, will look into it. Thanks! – Piskvor left the building Jun 12 '09 at 15:34
  • 1
    Mysql does not have that feature built-in. I'm told that Oracle has that (no help for you though) – Lathan Jul 26 '10 at 14:08
  • 18
    Unfortunately mysql-udf-regexp doesn't seem to have support for multibyte characters. regexp_replace('äöõü', 'ä', '') returns a long numeric string instead of real text. – lkraav Feb 20 '12 at 01:44
  • 1
    @lkraav Really? that thing sounds like a security leak since that "long numeric string" looks like some buffer overrun. – Earth Engine Mar 01 '13 at 00:48
  • 1
    Well this was a year ago, but I'm pretty sure I identified the issue correctly at the time. No idea if they've made any progress, since I haven't touched this tool since. – lkraav Mar 01 '13 at 00:51
  • 3
    MySQL itself does not support multi-byte characters with its RegEx features. – Brad Mar 20 '13 at 20:53
  • 4
    Windows users: The UDF Library linked here doesn't seem to have good windows support. The windows installation method outlined did not work well for me. – Jonathan Dec 05 '13 at 23:58
  • 2
    @lkraav you should try out the lib_mysqludf_preg library below as it works great. This the verbose version as it returns a blob by default and I don't know if you have a multibyte charset as your default: select cast( T.R as char) COLLATE utf8_unicode_ci from (select preg_replace('/ä/', '', 'öõüä') R ) T – gillyspy Feb 09 '14 at 19:46
  • Does the native REGEXP_REPLACE function allow back-references? – golimar Mar 26 '21 at 12:56
  • Nice, thanks --- I also got to a new realisation that UDFs are a real option. – Spastika Apr 01 '22 at 09:07
119

My brute force method to get this to work was just:

  1. Dump the table - mysqldump -u user -p database table > dump.sql
  2. Find and replace a couple patterns - find /path/to/dump.sql -type f -exec sed -i 's/old_string/new_string/g' {} \;, There are obviously other perl regeular expressions you could perform on the file as well.
  3. Import the table - mysqlimport -u user -p database table < dump.sql

If you want to make sure the string isn't elsewhere in your dataset, run a few regular expressions to make sure they all occur in a similar environment. It's also not that tough to create a backup before you run a replace, in case you accidentally destroy something that loses depth of information.

Ryan Ward Valverde
  • 6,458
  • 6
  • 37
  • 48
  • 37
    Okay, that should work, too; I didn't consider an offline replace. Nice out-of-the-box thinking there! – Piskvor left the building Feb 27 '12 at 05:33
  • 12
    Seems strange to me that you'd use find like that, I would shorten the command to sed -i 's/old_string/new_string/g' /path/to/dump.sql – speshak Mar 23 '12 at 16:17
  • 42
    Very risky, and unpractical with big data sets, or with referential integrity in place: for remove the data and then insert it again you will have to turn referential integrity off, leaving in practice your database off also. – Raul Luna May 15 '14 at 15:50
  • 5
    Having used this method in the past, I aggre with Raul, this is very risky. You need to be absolutely certain as well, that your string is not elswhere in your dataset. – eggmatters Jun 09 '15 at 16:56
  • 1
    Years late to the answer @speshak but the reason I chose to access the file like this was because I originally very nervous for the same reasons as mentioned above. At the time it seemed like separating the "find the file" part from the "replace" part would make the code easier to read before I submitted it – Ryan Ward Valverde Apr 19 '18 at 22:38
  • One more confirmation: I enjoyed this method, until I started to get errors about the number of columns not maching the expected value ( = broken the whole db / table). – CharlesM Aug 23 '18 at 09:36
  • I the the lesson is to be very careful about your regular expression. – Ryan Ward Valverde Oct 28 '21 at 14:56
117

With MySQL 8.0+ you could use natively REGEXP_REPLACE function.

12.5.2 Regular Expressions:

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

Replaces occurrences in the string expr that match the regular expression specified by the pattern pat with the replacement string repl, and returns the resulting string. If expr, pat, or repl is NULL, the return value is NULL.

and Regular expression support:

Previously, MySQL used the Henry Spencer regular expression library to support regular expression operators (REGEXP, RLIKE).

Regular expression support has been reimplemented using International Components for Unicode (ICU), which provides full Unicode support and is multibyte safe. The REGEXP_LIKE() function performs regular expression matching in the manner of the REGEXP and RLIKE operators, which now are synonyms for that function. In addition, the REGEXP_INSTR(), REGEXP_REPLACE(), and REGEXP_SUBSTR() functions are available to find match positions and perform substring substitution and extraction, respectively.

SELECT REGEXP_REPLACE('Stackoverflow','[A-Zf]','-',1,0,'c'); 
-- Output:
-tackover-low

DBFiddle Demo

Community
  • 1
  • 1
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
52

I recently wrote a MySQL function to replace strings using regular expressions. You could find my post at the following location:

http://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/

Here is the function code:

DELIMITER $$

CREATE FUNCTION  `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))
RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN 
 DECLARE temp VARCHAR(1000); 
 DECLARE ch VARCHAR(1); 
 DECLARE i INT;
 SET i = 1;
 SET temp = '';
 IF original REGEXP pattern THEN 
  loop_label: LOOP 
   IF i>CHAR_LENGTH(original) THEN
    LEAVE loop_label;  
   END IF;
   SET ch = SUBSTRING(original,i,1);
   IF NOT ch REGEXP pattern THEN
    SET temp = CONCAT(temp,ch);
   ELSE
    SET temp = CONCAT(temp,replacement);
   END IF;
   SET i=i+1;
  END LOOP;
 ELSE
  SET temp = original;
 END IF;
 RETURN temp;
END$$

DELIMITER ;

Example execution:

mysql> select regex_replace('[^a-zA-Z0-9\-]','','2my test3_text-to. check \\ my- sql (regular) ,expressions ._,');
joshweir
  • 5,427
  • 3
  • 39
  • 59
  • 29
    I'll just reinforce the above point: this function replaces *characters* that match a single-character expression. It says above that it is used "to repalce strings using regular expressions", and that can be a little misleading. It does its job, but it's not the job being asked for. (Not a complaint - it is just to save leading people down the wrong path) – Jason Feb 06 '12 at 23:15
  • 2
    Nice – but unfortunately doesn't deal with references like `select regex_replace('.*(abc).*','\1','noabcde')` (returns 'noabcde', not 'abc'). – Izzy Apr 02 '16 at 18:33
  • I've modified this method to attempt to address some of the limitations mentioned above and more. Please see [this answer](http://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql#38660574). – Steve Chambers Aug 01 '16 at 16:02
  • @Izzy The builtin MySQL 8+ REGEXP_REPLACE function doesn't either, right? I'm trying and it does not seem to work – golimar Mar 26 '21 at 12:55
  • @golimar I have no idea. And I cannot test either, as I don't have MySQL 8 or higher. – Izzy Mar 26 '21 at 19:15
47

we solve this problem without using regex this query replace only exact match string.

update employee set
employee_firstname = 
trim(REPLACE(concat(" ",employee_firstname," "),' jay ',' abc '))

Example:

emp_id employee_firstname

1 jay

2 jay ajay

3 jay

After executing query result:

emp_id employee_firstname

1 abc

2 abc ajay

3 abc

Community
  • 1
  • 1
Jay Patel
  • 481
  • 4
  • 2
  • @yellowmelon what are the two pairs of double quotes for? – codecowboy Mar 04 '16 at 12:31
  • 7
    He's padding the employeename with spaces before and after. This allows him to search-replace for (space)employeename(space), which avoids catching the employeename "jay" if its part of a larger string "ajay." Then he trims the spaces out when done. – Slam Apr 21 '16 at 20:29
25

UPDATE 2: A useful set of regex functions including REGEXP_REPLACE have now been provided in MySQL 8.0. This renders reading on unnecessary unless you're constrained to using an earlier version.


UPDATE 1: Have now made this into a blog post: http://stevettt.blogspot.co.uk/2018/02/a-mysql-regular-expression-replace.html


The following expands upon the function provided by Rasika Godawatte but trawls through all necessary substrings rather than just testing single characters:

-- ------------------------------------------------------------------------------------
-- USAGE
-- ------------------------------------------------------------------------------------
-- SELECT reg_replace(<subject>,
--                    <pattern>,
--                    <replacement>,
--                    <greedy>,
--                    <minMatchLen>,
--                    <maxMatchLen>);
-- where:
-- <subject> is the string to look in for doing the replacements
-- <pattern> is the regular expression to match against
-- <replacement> is the replacement string
-- <greedy> is TRUE for greedy matching or FALSE for non-greedy matching
-- <minMatchLen> specifies the minimum match length
-- <maxMatchLen> specifies the maximum match length
-- (minMatchLen and maxMatchLen are used to improve efficiency but are
--  optional and can be set to 0 or NULL if not known/required)
-- Example:
-- SELECT reg_replace(txt, '^[Tt][^ ]* ', 'a', TRUE, 2, 0) FROM tbl;
DROP FUNCTION IF EXISTS reg_replace;
DELIMITER //
CREATE FUNCTION reg_replace(subject VARCHAR(21845), pattern VARCHAR(21845),
  replacement VARCHAR(21845), greedy BOOLEAN, minMatchLen INT, maxMatchLen INT)
RETURNS VARCHAR(21845) DETERMINISTIC BEGIN 
  DECLARE result, subStr, usePattern VARCHAR(21845); 
  DECLARE startPos, prevStartPos, startInc, len, lenInc INT;
  IF subject REGEXP pattern THEN
    SET result = '';
    -- Sanitize input parameter values
    SET minMatchLen = IF(minMatchLen IS NULL OR minMatchLen < 1, 1, minMatchLen);
    SET maxMatchLen = IF(maxMatchLen IS NULL OR maxMatchLen < 1
                         OR maxMatchLen > CHAR_LENGTH(subject),
                         CHAR_LENGTH(subject), maxMatchLen);
    -- Set the pattern to use to match an entire string rather than part of a string
    SET usePattern = IF (LEFT(pattern, 1) = '^', pattern, CONCAT('^', pattern));
    SET usePattern = IF (RIGHT(pattern, 1) = '$', usePattern, CONCAT(usePattern, '$'));
    -- Set start position to 1 if pattern starts with ^ or doesn't end with $.
    IF LEFT(pattern, 1) = '^' OR RIGHT(pattern, 1) <> '$' THEN
      SET startPos = 1, startInc = 1;
    -- Otherwise (i.e. pattern ends with $ but doesn't start with ^): Set start pos
    -- to the min or max match length from the end (depending on "greedy" flag).
    ELSEIF greedy THEN
      SET startPos = CHAR_LENGTH(subject) - maxMatchLen + 1, startInc = 1;
    ELSE
      SET startPos = CHAR_LENGTH(subject) - minMatchLen + 1, startInc = -1;
    END IF;
    WHILE startPos >= 1 AND startPos <= CHAR_LENGTH(subject)
      AND startPos + minMatchLen - 1 <= CHAR_LENGTH(subject)
      AND !(LEFT(pattern, 1) = '^' AND startPos <> 1)
      AND !(RIGHT(pattern, 1) = '$'
            AND startPos + maxMatchLen - 1 < CHAR_LENGTH(subject)) DO
      -- Set start length to maximum if matching greedily or pattern ends with $.
      -- Otherwise set starting length to the minimum match length.
      IF greedy OR RIGHT(pattern, 1) = '$' THEN
        SET len = LEAST(CHAR_LENGTH(subject) - startPos + 1, maxMatchLen), lenInc = -1;
      ELSE
        SET len = minMatchLen, lenInc = 1;
      END IF;
      SET prevStartPos = startPos;
      lenLoop: WHILE len >= 1 AND len <= maxMatchLen
                 AND startPos + len - 1 <= CHAR_LENGTH(subject)
                 AND !(RIGHT(pattern, 1) = '$' 
                       AND startPos + len - 1 <> CHAR_LENGTH(subject)) DO
        SET subStr = SUBSTRING(subject, startPos, len);
        IF subStr REGEXP usePattern THEN
          SET result = IF(startInc = 1,
                          CONCAT(result, replacement), CONCAT(replacement, result));
          SET startPos = startPos + startInc * len;
          LEAVE lenLoop;
        END IF;
        SET len = len + lenInc;
      END WHILE;
      IF (startPos = prevStartPos) THEN
        SET result = IF(startInc = 1, CONCAT(result, SUBSTRING(subject, startPos, 1)),
                        CONCAT(SUBSTRING(subject, startPos, 1), result));
        SET startPos = startPos + startInc;
      END IF;
    END WHILE;
    IF startInc = 1 AND startPos <= CHAR_LENGTH(subject) THEN
      SET result = CONCAT(result, RIGHT(subject, CHAR_LENGTH(subject) + 1 - startPos));
    ELSEIF startInc = -1 AND startPos >= 1 THEN
      SET result = CONCAT(LEFT(subject, startPos), result);
    END IF;
  ELSE
    SET result = subject;
  END IF;
  RETURN result;
END//
DELIMITER ;

Demo

Rextester Demo

Limitations

  1. This method is of course going to take a while when the subject string is large. Update: Have now added minimum and maximum match length parameters for improved efficiency when these are known (zero = unknown/unlimited).
  2. It won't allow substitution of backreferences (e.g. \1, \2 etc.) to replace capturing groups. If this functionality is needed, please see this answer which attempts to provide a workaround by updating the function to allow a secondary find and replace within each found match (at the expense of increased complexity).
  3. If ^and/or $ is used in the pattern, they must be at the very start and very end respectively - e.g. patterns such as (^start|end$) are not supported.
  4. There is a "greedy" flag to specify whether the overall matching should be greedy or non-greedy. Combining greedy and lazy matching within a single regular expression (e.g. a.*?b.*) is not supported.

Usage Examples

The function has been used to answer the following StackOverflow questions:

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
  • 1
    Unlike the documentation for this function suggests, `null` can't be used in place of 0 in `maxMatchLength` or `minMatchLength` arguments, at least in mariadb 5.5.60 – gvlasov Mar 01 '21 at 12:07
  • 2
    Good spot - have now updated the SQL to allow `NULL` – Steve Chambers Mar 01 '21 at 12:55
17

I'm happy to report that since this question was asked, now there is a satisfactory answer! Take a look at this terrific package:

https://github.com/mysqludf/lib_mysqludf_preg

Sample SQL:

SELECT PREG_REPLACE('/(.*?)(fox)/' , 'dog' , 'the quick brown fox' ) AS demo;

I found the package from this blog post as linked on this question.

Community
  • 1
  • 1
dotancohen
  • 30,064
  • 36
  • 138
  • 197
10

You 'can' do it ... but it's not very wise ... this is about as daring as I'll try ... as far as full RegEx support your much better off using perl or the like.

UPDATE db.tbl
SET column = 
CASE 
WHEN column REGEXP '[[:<:]]WORD_TO_REPLACE[[:>:]]' 
THEN REPLACE(column,'WORD_TO_REPLACE','REPLACEMENT')
END 
WHERE column REGEXP '[[:<:]]WORD_TO_REPLACE[[:>:]]'
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
  • 1
    No, that won't work. Imagine your column contains 'asdfWORD_TO_REPLACE WORD_TO_REPLACE". Your method would result in 'asdfREPLACEMENT REPLACEMENT" where the correct answer would be "asdfWORD_TO_REPLACE REPLACEMENT". – Ryan Shillington Oct 03 '12 at 17:14
  • 1
    @Ryan ... that's exactly why I stated that it wasn't very wise ... in the use case you provide this would most definitely fail. In short it's a bad idea to use 'regex-like' structure. Even worse ... if you drop the where clause all your values will be NULL ... – Edward J Beckett Oct 03 '12 at 17:21
  • 1
    Actually Ryan in this case you're incorrect as the markers will only find matches for the zero-length word 'boundaries' so only words with boundaries before and after the word would match ... It's still a bad idea though ... – Edward J Beckett Oct 10 '12 at 23:33
8

I think there is an easy way to achieve this and It's working fine for me.

To SELECT rows using REGEX

SELECT * FROM `table_name` WHERE `column_name_to_find` REGEXP 'string-to-find'

To UPDATE rows using REGEX

UPDATE `table_name` SET column_name_to_find=REGEXP_REPLACE(column_name_to_find, 'string-to-find', 'string-to-replace') WHERE column_name_to_find REGEXP 'string-to-find'

REGEXP Reference: https://www.geeksforgeeks.org/mysql-regular-expressions-regexp/

Silambarasan R
  • 1,346
  • 15
  • 22
6

We can use IF condition in SELECT query as below:

Suppose that for anything with "ABC","ABC1","ABC2","ABC3",..., we want to replace with "ABC" then using REGEXP and IF() condition in the SELECT query, we can achieve this.

Syntax:

SELECT IF(column_name REGEXP 'ABC[0-9]$','ABC',column_name)
FROM table1 
WHERE column_name LIKE 'ABC%';

Example:

SELECT IF('ABC1' REGEXP 'ABC[0-9]$','ABC','ABC1');
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
user3796869
  • 231
  • 4
  • 11
  • Hello, thank you for the suggestion. I have been trying something similar, but the performance on my data sets has been unsatisfactory. For smallish sets, this may be viable. – Piskvor left the building Dec 01 '14 at 08:38
4

The one below basically finds the first match from the left and then replaces all occurences of it (tested in ).

Usage:

SELECT REGEX_REPLACE('dis ambiguity', 'dis[[:space:]]*ambiguity', 'disambiguity');

Implementation:

DELIMITER $$
CREATE FUNCTION REGEX_REPLACE(
  var_original VARCHAR(1000),
  var_pattern VARCHAR(1000),
  var_replacement VARCHAR(1000)
  ) RETURNS
    VARCHAR(1000)
  COMMENT 'Based on https://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/'
BEGIN
  DECLARE var_replaced VARCHAR(1000) DEFAULT var_original;
  DECLARE var_leftmost_match VARCHAR(1000) DEFAULT
    REGEX_CAPTURE_LEFTMOST(var_original, var_pattern);
    WHILE var_leftmost_match IS NOT NULL DO
      IF var_replacement <> var_leftmost_match THEN
        SET var_replaced = REPLACE(var_replaced, var_leftmost_match, var_replacement);
        SET var_leftmost_match = REGEX_CAPTURE_LEFTMOST(var_replaced, var_pattern);
        ELSE
          SET var_leftmost_match = NULL;
        END IF;
      END WHILE;
  RETURN var_replaced;
END $$
DELIMITER ;

DELIMITER $$
CREATE FUNCTION REGEX_CAPTURE_LEFTMOST(
  var_original VARCHAR(1000),
  var_pattern VARCHAR(1000)
  ) RETURNS
    VARCHAR(1000)
  COMMENT '
  Captures the leftmost substring that matches the [var_pattern]
  IN [var_original], OR NULL if no match.
  '
BEGIN
  DECLARE var_temp_l VARCHAR(1000);
  DECLARE var_temp_r VARCHAR(1000);
  DECLARE var_left_trim_index INT;
  DECLARE var_right_trim_index INT;
  SET var_left_trim_index = 1;
  SET var_right_trim_index = 1;
  SET var_temp_l = '';
  SET var_temp_r = '';
  WHILE (CHAR_LENGTH(var_original) >= var_left_trim_index) DO
    SET var_temp_l = LEFT(var_original, var_left_trim_index);
    IF var_temp_l REGEXP var_pattern THEN
      WHILE (CHAR_LENGTH(var_temp_l) >= var_right_trim_index) DO
        SET var_temp_r = RIGHT(var_temp_l, var_right_trim_index);
        IF var_temp_r REGEXP var_pattern THEN
          RETURN var_temp_r;
          END IF;
        SET var_right_trim_index = var_right_trim_index + 1;
        END WHILE;
      END IF;
    SET var_left_trim_index = var_left_trim_index + 1;
    END WHILE;
  RETURN NULL;
END $$
DELIMITER ;
Nae
  • 14,209
  • 7
  • 52
  • 79
0

Yes, you can.

UPDATE table_name 
  SET column_name = 'seach_str_name'
  WHERE column_name REGEXP '[^a-zA-Z0-9()_ .\-]';
janw
  • 8,758
  • 11
  • 40
  • 62
Maksym Dudyk
  • 1,082
  • 14
  • 16