简体   繁体   中英

php preg_match() regular expression

I am working on SEO thing in project to match best URL from possible url's, so i am trying to match request url using preg_match() function from url pattern

can anyone please help me create regular expression to match only specific urls from all urls, i am newbie in Regular expression, please see my stuff

following 6 urls,

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

i want to match 1,2,3 urls, other 3 invalid

i create this regular expression but it's does not working

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

I don't really see all the point of your request.

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

This will resolve your 1,2,3 but not the 4,5,6

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

This regex might work for you:

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

RegEx Demo

This is 100% working:

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

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

Here isn't a way to cheat around it

If you wanted to capture the whole string

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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