PHP: Regex: Match if doesnt contain


PHP: Regex: Match if doesnt contain

我有两个url(如下)。我需要匹配一个不包含"故事"部分的。我知道我需要使用负面表情,但我一辈子都不能让它工作

Urls

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

当前Regex

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

示例:

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

你可以试试这个:

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

您可以像这样使用负前瞻

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

RegEx演示

(?!.*'bstory'b)负前瞻,如果URL中有单词story,它将停止匹配。

如果不必使用regex ,可以使用strpos()进行检查

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