查找没有结尾斜杠的 url 模式 [英] find pattern for url with no ending slash

查看:54
本文介绍了查找没有结尾斜杠的 url 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 preg_match_all 模式来查找页面上没有尾部斜杠的所有 URL.

I'm looking for preg_match_all pattern to find all URL on a page that don't have trailing slash.

例如:如果我有

  1. a href="/testing/abc/">以斜线结尾

  1. a href="/testing/abc/">end with slash

a href="/testing/test/mnl">没有结尾斜线

a href="/testing/test/mnl">no ending slash

结果是#2

谢谢.

推荐答案

最好使用 DOM 解析器提取所有的 href 链接,并查看 URL 是否以斜杠结尾.不需要正则表达式.

Better extract all your href links using DOM parser and see if URL is ending with slash or not. No regex needed for that.

对于提供的示例的正则表达式解决方案,您可以使用此正则表达式:

For the regex solution for the examples provided you can use this regex:

/href=(['"])[^\s]+(?<!\/)\1/

现场演示:http://www.rubular.com/r/f2XJ6rF5FbH3>

说明:

href=   -> match text href=
(['"])  -> match single or double quote and create a group #1 with this match
[^\s]+  -> match 1 or more character until a space is found
(?<!\/) -> (negative lookbehind) only match if is not preceded by /
\1      -> match closing single or double quote (group #1)

这篇关于查找没有结尾斜杠的 url 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆