繁体   English   中英

PHP preg_match()正则表达式

[英]php preg_match() regular expression

我正在研究项目中的SEO事物,以匹配可能来自url的最佳URL,所以我正尝试使用url模式中的preg_match()函数来匹配请求url。

谁能帮我创建正则表达式以匹配所有URL中的特定URL,我是正则表达式的新手,请参阅我的东西

以下6个网址,

1) http://domain.com/pressrelease
2) http://domain.com/pressrelease/
3) http://domain.com/pressrelease/index/1
4) http://domain.com/pressrelease/index/1/1/1
5) http://domain.com/pressrelease/inde2x/
6) http://domain.com/pressrelease/inde2x/2

我想匹配1,2,3网址,其他3个无效

我创建了这个正则表达式,但是它不起作用

(\/pressrelease\/)+((?!index).)*$

我真的看不到您的要求。

(@ ^ http [s]?:// [^ /] / [^ /] ?(/?(index / [^ /])?)?$ @)

这将解决您的1,2,3,但不能解决4,5,6

https://regex101.com/r/fQ0hZ8/2

此正则表达式可能适合您:

(/pressrelease)(/index/\d+)?/?$

正则演示

这是100%正常工作:

/(\/pressrelease)(\/)?((index))?(\/1)?$/m

https://regex101.com/r/kD9uD9/1

这不是欺骗它的方法

如果您想捕获整个字符串

(http:\/\/.*pressrelease(()|(\/)|(\/index)|(\/index\/[^\/]*)))$

https://regex101.com/r/aI0fB1/1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM