MySQL (MariaDB 10.1) 使用 REGEX 查找和 replace/delete 字符串
MySQL (MariaDB 10.1) find and replace/delete string with REGEX
我有一个产品备注栏,其中许多包含以下内容(以及其他信息):
<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on 02-17-2017. May still be available.</strong></em></p>
我更改的日期是 02-17-2017(但将始终采用该格式)。我可以仅通过数据库查询查找并删除这些字符串吗(保留列中的其余数据,如 MySQL REPLACE() 但使用正则表达式来匹配日期)还是我需要这样做在 PHP?
WHERE notes LIKE '<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on %. May still be available.</strong></em></p>'
注意我在字符串中放了一个%
。
我不明白"delete those strings" -- 你想用空字符串替换它们吗? NULL
?删除包含在 notes
列中的 行 ?还有别的吗?我希望 WHERE
子句足以让您朝着正确的方向前进。
注:REGEXP
为'overkill'; LIKE
足够了。
如果您想删除日期但保留字符串的其余部分,那么这样做不行吗?...
UPDATE tbl
SET notes = '<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on xxxxxxxxxxx. May still be available.</strong></em></p>'
WHERE notes LIKE '<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on %. May still be available.</strong></em></p>';
(或者任何您想作为占位符的内容。)
MariaDB-10.0.5 有这些:REGEXP_REPLACE()、REGEXP_INSTR() 和 REGEXP_SUBSTR();但我看不出它们对你的情况是必要的。
我能够做到:
UPDATE `ii_Product`
SET `notes`= REGEXP_REPLACE(`notes`, '\n?<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on .*\. May still be available.<\/strong><\/em><\/p>\n?\n?', '')
我有一个产品备注栏,其中许多包含以下内容(以及其他信息):
<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on 02-17-2017. May still be available.</strong></em></p>
我更改的日期是 02-17-2017(但将始终采用该格式)。我可以仅通过数据库查询查找并删除这些字符串吗(保留列中的其余数据,如 MySQL REPLACE() 但使用正则表达式来匹配日期)还是我需要这样做在 PHP?
WHERE notes LIKE '<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on %. May still be available.</strong></em></p>'
注意我在字符串中放了一个%
。
我不明白"delete those strings" -- 你想用空字符串替换它们吗? NULL
?删除包含在 notes
列中的 行 ?还有别的吗?我希望 WHERE
子句足以让您朝着正确的方向前进。
注:REGEXP
为'overkill'; LIKE
足够了。
如果您想删除日期但保留字符串的其余部分,那么这样做不行吗?...
UPDATE tbl
SET notes = '<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on xxxxxxxxxxx. May still be available.</strong></em></p>'
WHERE notes LIKE '<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on %. May still be available.</strong></em></p>';
(或者任何您想作为占位符的内容。)
MariaDB-10.0.5 有这些:REGEXP_REPLACE()、REGEXP_INSTR() 和 REGEXP_SUBSTR();但我看不出它们对你的情况是必要的。
我能够做到:
UPDATE `ii_Product`
SET `notes`= REGEXP_REPLACE(`notes`, '\n?<p><em><strong>Item dropped from ESP datafeed and pricing removed from site on .*\. May still be available.<\/strong><\/em><\/p>\n?\n?', '')