Regular Express - Pattern matching

How to include everything after /blog but shouldn't include words like wp-content and wp-includes?

Currently showing all using regex:

^/blog/*

I tried this but no luck:

^/blog/(!(wp-content&&wp-includes))*

Urls:

example.com/blog/content (should include)
example.com/blog/page/2 (should include)
example.com/blog/wp-content/ (shouldn't include)
example.com/blog/wp-includes/ (shouldn't include) 

Answers


Your first regex

^/blog/*

Means a string, starting with /blog that has zero or more forward slashes after that. I'm guessing you meant just

^/blog

or

^/blog/.*$

As for how to exclude those two words:

^/blog(.(?!wp-content|wp-includes))*$

The idea is to advance one character at a time (.), checking that wp-content or wp-includes doesn't follow after that using negative lookahead ((?!)) til the end of the string ($).


If you want to ensure the slash after blog is there without repeating the regex, you can put the negative lookahead first and then start matching characters:

^/blog/(?!.*(wp-content|wp-includes)).*$

This first ensures that there is no wp-content or wp-includes after any amout of characters (.*) and then continues matching.


you can use negative lookahead like this:

/blog(/(?!wp-content|wp-includes).*)

if the semantics of the match is "inside a url", then you most likely only want to match non-whitespace characters:

/blog(/(?!wp-content|wp-includes)\S*)

and if there are more than the 2 "wp-" examples:

/blog(/(?!wp-)\S*)

..not using ^ because your examples do not match at the beginning of the string

https://regex101.com/r/gP4eZ7


Need Your Help

Xcode UI tests - Lost connection to test manager service

ios xcode swift xcode-ui-testing

Sometimes while running my UI tests, I get the following error Lost connection to test manager services. No logs or anything. Happens randomly. I would provide more information but I just don't hav...

Chrome block requests

google-chrome

I am facing unusual issue in Chrome. it is blocking a few requests and returns following: