gpt4 book ai didi

mysql - REGEXP_REPLACE 指南

转载 作者:可可西里 更新时间:2023-11-01 06:37:41 26 4
gpt4 key购买 nike

我一直在尝试批量删除 Wordpress 帖子中的垃圾链接,如下所示:

<a style="text-decoration: none" href="/price-of-xenical-at-pharmacy">.</a>

它们位于 post_content 列下的 wp_posts 表中。我试图通过在 href 标记中添加 % 的通配符来做到这一点,因为所有 URL 都不同,但 anchor (句号)和内联样式是相同的。

UPDATE wp_posts
SET post_content = REPLACE (post_content,
'<a style="text-decoration:none" href="%">.</a>',
'.');

后来有人告诉我,SQL 不支持我正在尝试做的事情(或者至少不支持我正在做的事情)。

我正在使用显然支持 REGEXP_REPLACE 的 MariaDB ,所以我正在寻找一些关于我需要什么 SQL 查询和正则表达式来大量删除这些链接但保持所有其他内容不变的指导。

非常感谢任何帮助,目的是删除上述字符串,或替换为空格

更新

示例帖子内容,最后一个链接是我需要删除的类型。 :

    <h2>Warranty</h2>
<span style="font-size: small"> </span>

<span style="font-size: small">Lorem ipsum dolor sit amet, non risus bibendum quis morbi, duis elit porttitor semper, ante augue at consectetuer elit lectus est, nascetur neque consequuntur donec turpis. Cursus ullamcorper posuere massa interdum, rhoncus blandit, vitae in etiam justo lectus eu fames. Dolor quam dicta wisi class duis. Eleifend sagittis, scelerisque convallis consectetuer sed non aptent. Velit tristique vulputate proin, ipsum diam aliquam. Nibh sit vitae et m</span>

&nbsp;

<a href="https://www.example.com/wp-content/image.jpg"><img class="alignright size-full wp-image-56" title="image" src="https://www.example.com/wp-content/image.jpg" alt="image" width="280" height="280" /></a><a style="text-decoration: none" href="/price-of-xenical-at-pharmacy">.</a>

最佳答案

如果你想删除所有 anchor 标签,但保留标签中的文本,请尝试使用此模式:

<a[^>]*>(.*?)</a>

然后,仅替换为第一个捕获组。除了我们使用 (.*?) 来捕获 anchor 标记之间的内容之外,关于该模式没有太多要说的。 .*? 很重要,它告诉正则表达式引擎在第一个 结束标记处停止。否则,如果我们只使用 (.*),它可能会消耗多个 anchor 标记,如果它们存在于您的列中的话。

SELECT
REGEXP_REPLACE('<a style="text-decoration:none" href="[^"]*">BLAH</a>',
'<a[^>]*>(.*?)</a>', '$1');

上面的查询输出BLAH

如果你只想去掉所有的 anchor 标签,那么使用这个:

SELECT
REGEXP_REPLACE('<a style="text-decoration:none" href="[^"]*">BLAH</a>',
'<a[^>]*>(.*?)</a>', '');

关于mysql - REGEXP_REPLACE 指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com