MySQL 正则表达式,用于查找正文中带有双斜杠的内部 URL


MySQL Regular Expression for Finding internal URL's with a Double Slash in the Body

我们有一个使用wordpress的网站,我们发现在某些时候,一个错误的插件或用户错误在siteurl后面添加了双斜杠(例如,http://example.site//category1/http://example.site/category1//category2/等。

这似乎有效,但看起来没有足够的结果。

SELECT id, post_content
FROM `wp_posts`
where post_content
regexp '(href="[^"]*[^:]'/'/[^"]*)'
and post_status  in('draft','publish')
order by id asc

有没有更好的方法可以做到这一点?我不希望它在 http: 之后的双斜杠上匹配,因此在 : 上匹配负数。

编辑:为了澄清起见,我想找到所有帖子(wordpress帖子/页面的正文),这些帖子的网址硬编码到具有双斜杠的页面中,但在http:之后的双斜杠上不匹配。

正则表达式应在以下方面匹配: http://example.site//category1/http://example.site/category1//category2/,甚至http://example.site/category1/category2//example.site/category1//category2/

但不应在以下方面匹配: http://example.site/category1/http://example.site/category1/category2/

也许这样的事情会起作用。

SELECT *
FROM wp_posts
WHERE CASE WHEN instr(post_content,'http://') > 0 THEN 
  substring(post_content,7) regexp ''/'/'
ELSE
  post_content regexp ''/'/'
END

这是SQL小提琴。

祝你好运。

您可以使用:

regexp '(https?:'/'/|www'.)[^ ]*'/'/'

如果帖子包含http[s]://www.后跟//的非空格字符,这将匹配该帖子。

请参阅此 SQLFiddle(改编自 sgeddes 的小提琴)。

或者您可以将正则表达式减少到'[^:]'/'/'并查找包含该内容的帖子。