簡體   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