简体   繁体   English

pcre(php)正则表达式以匹配url

[英]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 我正在研究项目中的SEO事物,以匹配可能来自url的最佳URL,所以我正尝试使用url模式中的preg_match()函数来匹配请求url。

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 谁能帮我创建正则表达式以匹配所有URL中的特定URL,我是正则表达式的新手,请参阅我的东西

following 3 urls 以下3个网址

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

i want to match 2,3 urls, urls have not contain of index/(:num) after pressrelease 我想匹配2,3网址, pressrelease后网址中不包含index/(:num)

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. 由于您要将正则表达式传递给preg_match ,因此下面的正则表达式就可以了。

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

DEMO 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 . (?!index\\/\\d+\\b)否定超前断言,断言匹配/pressrelease/后面不会是index/number这样的格式的字符串。

It actually works, but it doesn't match the first part of the URL. 它实际上有效,但是与URL的第一部分不匹配。

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

Take a look at this demo on Rubular to check it. Rubular观看此演示以进行检查。

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

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