mysql正则替换

2017/04/14 mysql
update table set name = replace(name, substring(name, locate('<contact>', name),locate('</contact>', name)-locate('<contact>', name)+10),'');   //+10为字符串'</contact>'的长度

下面描述下,所用到的函数: locate:

LOCATE(substr,str) POSITION(substr IN str) 返回子串 substr 在字符串 str 中第一次出现的位置。如果子串 substr 在 str 中不存在,返回值为 0:

substring

SUBSTR(str,pos,len): 由中的第位置开始,选出接下去的个字元。

replace

replace(str1, str2, str3): 在字串 str1 中,當 str2 出現時,將其以 str3 替代。

http://img[0-9].91huo.cn/zm/ => http://zm.91.com/upload/

http://img[0-9].91huo.cn/zs/ => http://zs.91.com/upload/

http://img[0-9].91huo.cn/s/ => http://s.zm.91.com/upload/

update dede_91lock_1114 set NEWSPIC = replace(NEWSPIC, substring(NEWSPIC, locate(‘://’, NEWSPIC),locate(‘/zm/’, NEWSPIC)-locate(‘://’, NEWSPIC)+4),’://zm.91.com/upload/’);

update dede_91lock_1114 set NEWSPIC = replace(NEWSPIC, substring(NEWSPIC, locate(‘://’, NEWSPIC),locate(‘/zs/’, NEWSPIC)-locate(‘://’, NEWSPIC)+4),’://zs.91.com/upload/’);

update dede_91lock_1114 set NEWSPIC = replace(NEWSPIC, substring(NEWSPIC, locate(‘://’, NEWSPIC),locate(‘/s/’, NEWSPIC)-locate(‘://’, NEWSPIC)+3),’://s.zm.91.com/upload/’);


Search

    Table of Contents