B-rad
B-rad

Reputation: 186

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

Upvotes: 1

Views: 60

Answers (3)

jmleroux
jmleroux

Reputation: 947

you can try this one :

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

Upvotes: 1

geoandri
geoandri

Reputation: 2428

You can check with strpos() if you don't have to use regex

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

Upvotes: 1

anubhava
anubhava

Reputation: 784998

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.

Upvotes: 1

Related Questions