Regex to find pattern STRING/STRING, with word exception

Viewed 35

I am working with urls. I need to find a regex that catches strings like this:

string/string/ -OR- string/string

But there is an exception. The first string cannot be "brand" nor "category" (but the second can be).

The second forward slash is optional.

So these are valid strings:

string/string-2
string/yes/
string/brand   <--- "brand" or "category" can be after the first slash but not before
string/category/  <--- "brand" or "category" can be after the first slash but not before
the-string/hello
the-string/the-string-2/

These are invalid strings:

hello <--- Only 1 string, has to be 2, not good
hello/ <--- Only 1 string, has to be 2, not good
brand/string   <--- contains "brand" before the first slash, not good
brand/the-string/   <--- contains "brand" before the first slash, not good
category/string/   <--- contains "category" before the first slash, not good
category/hello   <--- contains "category" before the first slash, not good
hey/hello/world <--- Too many strings (3),only 2 needed, not good. ***THIS IS WHERE I AM STUCK***

So far I am trying this:

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

But it is still catching 3 strigs, like this "hey/hello/world", I need only two (string/string -OR- string/string/)

Please help!

0 Answers
Related