简体   繁体   English

使用正则表达式在MySql中搜索和替换

[英]Search & replace in MySql, using regex

I have a row with content that can look like this: 我有一排内容可能如下所示:

bla bla bla [ATTACH]123456[/ATTACH] bla bla bla
bla bla bla [ATTACH]78912[/ATTACH] bla something bla

I need to search the row for all occurences of [ATTACH]number[/ATTACH] and replace it like this: 我需要在该行中搜索所有出现的[ATTACH] number [/ ATTACH],并将其替换为:

[ATTACH]123456[/ATTACH] should become [sharedmedia=core:attachments:123456] [ATTACH]123456[/ATTACH]应该成为[sharedmedia=core:attachments:123456]

or... 要么...

[ATTACH]78912[/ATTACH] should become [sharedmedia=core:attachments:78912] [ATTACH]78912[/ATTACH]应该成为[sharedmedia=core:attachments:78912]

SQL is not optimized for this sort of work. SQL并未针对此类工作进行优化。 The following might accomplish what you want: 以下可能完成您想要的:

update table t
    set content = replace(replace(content, '[ATTACH]', '[sharedmedia=core:attachments:'
                                 ), '[/ATTACH]', ']'
                         )
    where content like '[ATTACH]%[/ATTACH]';

This assumes that all occurrence of [ATTACH] are followed by a number. 假定[ATTACH]所有出现均带有数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM