mysql 中的正则表达式
mysql 富文本内容替换
字符串方式
UPDATE `child_course` SET promotional_photos=replace(promotional_photos, '<img src="../jeecg-boot/', '<img src="/jeecg-boot/');
正则表达式方式
UPDATE `child_course` SET promotional_photos=REGEXP_REPLACE(promotional_photos, 'height=\\".*\\"', '');
注意上面的方式是在8.0之后的!如果使用5.7的版本可以参考以下解决方法
1、开启mysql自定义函数支持
show variables like '%fun%';
log_bin_trust_function_creators=OFF表示没有开启自定义函数。输入开启命令。
set global log_bin_trust_function_creators=1;
注:此处不开启,后面自定义函数写好。调用不起作用 !执行可能需要root权限
2.编写regexp_replace()自定义函数
#创建前删除已经创建的自定义函数
drop function if exists regexp_replace;
#创建 regexp_replace函数
DELIMITER $$
CREATE FUNCTION `regexp_replace`(string_a text,pattern VARCHAR(1000),string_b VARCHAR(1000))
RETURNS text
DETERMINISTIC
BEGIN
DECLARE string_c text;
DECLARE nub VARCHAR(1);
DECLARE i INT;
SET i =1;
SET string_c ='';
IF string_a REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(string_a) THEN
LEAVE loop_label;
END IF;
SET nub = SUBSTRING(string_a,i,1);
IF NOT nub REGEXP pattern THEN
SET string_c = CONCAT(string_c,nub);
ELSE
SET string_c = CONCAT(string_c,string_b);
END IF;
SET i=i+1;
END LOOP;
ELSE
SET string_c = string_a;
END IF;
RETURN string_c;
END$$
DELIMITER;
show FUNCTION STATUS