Ran*_*r11 7 regex mysql mariadb
我一直在尝试从Wordpress上批量删除垃圾链接,如下所示:
<a style="text-decoration: none" href="/price-of-xenical-at-pharmacy">.</a>
它们位于post_content列下的wp_posts表中。我试图通过在href标记内添加%的通配符来完成此操作,因为所有URL都不同,但是锚点(句号)和内联样式相同。
UPDATE wp_posts
SET post_content = REPLACE (post_content,
'<a style="text-decoration:none" href="%">.</a>',
'.');
Run Code Online (Sandbox Code Playgroud)
从那以后,我被告知SQL不支持我正在尝试做的事情(或者至少不支持我做的事情)。
我正在使用显然支持REGEXP_REPLACE的 MariaDB ,因此我正在寻找有关大规模删除这些链接但保留所有其他内容的SQL查询和REGEX的一些指导。
任何帮助,我们将不胜感激,其目的是删除上述字符串,或将其替换为空格
示例帖子内容,最后一个链接是我需要删除的类型。:
<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>
<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>
Run Code Online (Sandbox Code Playgroud)
如果您想删除所有锚标记,但保留标记中包含的文本,请尝试使用此模式:
<a[^>]*>(.*?)</a>
Run Code Online (Sandbox Code Playgroud)
然后,仅替换为第一个捕获组。(.*?)除了我们用来捕获锚标记之间的内容之外,关于该模式没有太多可说的。很重要,它告诉正则表达式引擎在第一个.*?结束标记处停止。否则,如果我们只是使用,它可能会消耗多个锚标记(如果它们存在于您的列中)。(.*)
SELECT
REGEXP_REPLACE('<a style="text-decoration:none" href="[^"]*">BLAH</a>',
'<a[^>]*>(.*?)</a>', '$1');
Run Code Online (Sandbox Code Playgroud)
上述查询输出BLAH。
如果您只想删除所有锚标记,请使用以下命令:
SELECT
REGEXP_REPLACE('<a style="text-decoration:none" href="[^"]*">BLAH</a>',
'<a[^>]*>(.*?)</a>', '');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
241 次 |
| 最近记录: |