如果 (.*) 包含字符串,则正则表达式不匹配

Regex do not match if (.*) contains a string

提问人:johnjon432183556 提问时间:9/4/2022 最后编辑:RavinderSingh13johnjon432183556 更新时间:9/4/2022 访问量:164

问:

我正在尝试匹配 URL 中的字符串及其后面的所有字符串,除非它后面的内容包含某个字符串。

我试过了:

(?:/path/)(.*)

示例字符串:

//https://example.com/path/css/style (match)    
//https://example.com/path/css/style2 (match)
//https://example.com/path/css/notstyle (match)(but I'm looking for it to not)
正则 模式 匹配表达式

评论

0赞 lucas_7_94 9/4/2022
它是否总是以“style”或“styleX”结尾,其中 X 是一个数字?
0赞 johnjon432183556 9/4/2022
不,这只是一个例子。我正在尝试获取“/path/”之后的路径,除非 (.*) 后面的部分包含“notstyle”
0赞 Poul Bak 9/4/2022
告诉我们您正在使用哪种正则表达式引擎/编程语言,以便我们制作适合您的正则表达式。

答:

1赞 The fourth bird 9/4/2022 #1

如果要匹配,可以使用/path/css/style

https?://\S*/path/css/style\S*

正则表达式演示

如果要排除,可以在匹配协议后使用否定的 lookahead:notstyle

https?://(?!\S*notstyle)\S*/path/\S*

正则表达式演示