[英]REGEX_REPLACE in mySQL everything inside character with its value
I'd like to replace a text field in mysql using REGEX_REPLACE.我想使用 REGEX_REPLACE 替换 mysql 中的文本字段。
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:所以 output 应该是这样的:
Hi friends @user@contoso.com, what are you doing?
My SQL Code:我的 SQL 代码:
UPDATE `Chat`
SET `Text` = REGEXP_REPLACE(`Text`, '\<(.*?)\>', 'text inside <>');
Link to dbfiddle:链接到 dbfiddle:
https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=f930852c6bf05fbb31125f42852093f9 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. \\1
是转义的\1
以引用第一个捕获组值。 You can use that to reference the captured value in the replacement.您可以使用它来引用替换中捕获的值。 I modified your regex to match the brackets as well.
我也修改了您的正则表达式以匹配括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.