匹配带有特定签名的 URL
我有这个 preg_match 规则:
preg_match( '#(http:\/\/(www.)?imgur.com)\/(gallery\/)?(([-|~_0-9A-Za-z]+)&?.*?)#i', $link, $matches )
这与这些匹配:
http://imgur.com/xxxx
http://www.imgur.com/xxxx
http://imgur.com/gallery/xxxx
如何避免与 imgur.com/a/xxxx#xxxx
匹配?
I have this preg_match rule:
preg_match( '#(http:\/\/(www.)?imgur.com)\/(gallery\/)?(([-|~_0-9A-Za-z]+)&?.*?)#i', $link, $matches )
this matches these:
http://imgur.com/xxxx
http://www.imgur.com/xxxx
http://imgur.com/gallery/xxxx
How I can avoid to match with imgur.com/a/xxxx#xxxx
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否是您想要的:
使用
http://imgur.com/a/xxxxxx
这样的网址将不匹配。PS:虽然取决于你在做什么,但我认为正则表达式可能会变得不必要的复杂,你应该尝试找到更简单的方法。看看我在这里关于解析网址的答案:会正则表达式最适合这个问题吗?
I am not sure if this is what you want:
With this url like
http://imgur.com/a/xxxxxx
won't match.PS: Though depending on what you are doing, I think that the regular expression may get unnecessarily complicated and you should try to find simpler methods. Look at my answer here regarding parsing urls: Would a regular expression be best for this problem?