Home > Back-end >  Regex do not match if (.*) contains a string
Regex do not match if (.*) contains a string

Time:09-05

I'm trying to match a string in a URL and all that comes after it unless what comes after it contains a certain string.

I tried:

(?:/path/)(.*)

Example strings:

//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)

CodePudding user response:

If your regular expression parser supports it, you can use a negative lookbehind on the end of the URL.

(?:/path/)(.*)(?<!notstyle)$

CodePudding user response:

If you want to match /path/css/style you might use

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

Regex demo

If you want to exclude notstyle you can use a negative lookahead after the matching the protocol:

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

Regex demo

  • Related