Regex to match urls with 2 subdirectories deep only

Viewed 41

I need to match urls with 2 directories deep only and optional forward slash at the end:

Also, the first directory has 2 exceptions, it cannot be "brand" or "category".

These are invalid:

https://mywebsite.com/brand/canon <--- has prohibited word "brand" in the 1st dir
https://mywebsite.com/category/canon  <--- has prohibited word "category" in the 1st dir
https://mywebsite.com/classifieds  <--- only 1 dir
https://mywebsite.com/classified/job/today  <--- more than 2 dirs
https://mywebsite.com/classified/job/today/   <--- more than 2 dirs

These are valid:

https://mywebsite.com/selection/canon <--- 2 dirs, perfect
https://mywebsite.com/xerox/brand <--- 2 dirs, perfect. It has "brand" but at the 2nd dir
https://mywebsite.com/offers/printers/ <--- perfect

I am trying this:

^(.*\b(?<!\bbrand|category))/(.*)?

But it is still matching 3+ directories deep. I need exactly 2 dirs with optional forward slash.

Please help

0 Answers
Related