简体   繁体   中英

PHP: Regex: Match if doesnt contain

I have two urls (below). I need to match the one that doesn't contain the "story" part in the it. I know i need to use negative lookhead/behind, but i cant for the life of me get it to work

Urls

news/tech/story/2014/oct/28/apple-iphone/261736

news/tech/2014/oct/28/apple-iphone/261736

Current Regex

news\/([a-z0-9-\/]{1,255})(\d{4})\/(\w{3})\/(\d{2})\/([a-z0-9\-]{1,50})\/(\d{1,10})

Example:

http://regex101.com/r/jC7jC4/1

You can use negative lookahead like this:

(?!.*\bstory\b)news\/([a-z0-9-\/]{1,255})(\d{4})\/(\w{3})\/(\d{2})\/([a-z0-9\-]{1,50})\/(\d{1,10})

RegEx Demo

(?!.*\\bstory\\b) is negative lookahead that will stop match if there is a word story in the URL.

你可以尝试这个:

news\/(([a-z0-9-\/](?!story)){1,255})(\d{4})\/(\w{3})\/(\d{2})\/([a-z0-9\-]{1,50})\/(\d{1,10})

如果您不必使用正则表达式,可以使用strpos()进行检查

 if (strpos($url, 'story') === false

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