简体   繁体   中英

pcre(php) regular expression to match url

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 3 urls

1). http://domain.com/pressrelease/index/1
2). http://domain.com/pressrelease/123
3). http://domain.com/pressrelease/blah

i want to match 2,3 urls, urls have not contain of index/(:num) after pressrelease

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

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

Since you're passing the regex to preg_match , the below regex would be fine.

\/pressrelease\/(?!index\/\d+\b).*

DEMO

(?!index\\/\\d+\\b) negative lookahead assertion which asserts that the match /pressrelease/ won't be followed by the string which is in the format like index/number .

It actually works, but it doesn't match the first part of the URL.

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

Take a look at this demo on Rubular to check it.

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