简体   繁体   中英

REGEX_REPLACE in mySQL everything inside character with its value

I'd like to replace a text field in mysql using REGEX_REPLACE.

My string looks like this:

Hi friends @[Friendly User <user@contoso.com>], what are you doing?

What I want is, to remove everything inside the brackets [] and except the text inside the <> .

So the output should look like this:

Hi friends @user@contoso.com, what are you doing?

My SQL Code:

UPDATE `Chat`
SET `Text` = REGEXP_REPLACE(`Text`, '\<(.*?)\>', 'text inside <>');

Link to dbfiddle:

https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=f930852c6bf05fbb31125f42852093f9

UPDATE `Chat`
SET `Text` = REGEXP_REPLACE(`Text`, '\\[.*\<(.*?)\>.*\\]', '\\1');

\\1 is an escaped \1 to reference the first capture groups value. You can use that to reference the captured value in the replacement. I modified your regex to match the brackets as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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