Hello,
I'm hoping for some help with a regex that has me stumped. My skill with regex is rather rudimentary.
To start, here are a couple of example URLs, and whether I want to match them or not:

collection/col1 // MATCH
collection/col2 // FAIL
collection/col1/products/pro1 // FAIL

My criteria:

  • One single regex (cannot be multiple expressions)
  • I have a list of "col"s to fail on. For example, I do not want to match if specifically "col2" or "col3" are in the URL
  • Aside from col2 or col3, i can have any col[#] in the URL
  • Even if a valid col# is in the URL, the expression MUST fail if /products/ appears anywhere. The URL structure is such that if /products/ appears anywhere, it will always be after /collections/col#

Here are expressions I've come up with, but I'm missing how to make the expression definitely fail if /products/ is anywhere in it. Is this possible?

collections\/(?!col2|col3)(?!\/products\/)
collections\/{?!col2|col3)[0-9a-zA-Z-_]+(?!\/products\/)

// And like 200 other things I tried that I didn't record

Thank you for any assistance.

Try collections\/col(\d{2,}|[^23])(?!\/products), because (\d{2,}|[^23]) will allow 2 or more numbers (as in col22, col21) or 1 number other than 2 or 3 (col1, col4, col5 but not col2 nor col3).

Thanks for the reply, xrj. I should have been more clear though -- my real problem here is the /products/ part. The "col1" part is just for placeholders. Really the URL looks like, say,

collections/shirts/products/tee-shirt
collections/tea/products/green-tea
collections/coffee
collections/coffee/products/cup-of-joe

So if I want to exclude anything under the "shirts" or "tea" collections, I can do

collections\/(?!shirts|tea)

Now the coffee collection is fine, but it's not OK if the URL has "/products/" in it. But I'm finding that...

collections\/(?!shirts|tea)(?!\/products\/)

...doesn't work. I believe this has to do with that lookaheads don't move along the string, I'm just not sure where to go from there.

Assuming products (if present) is after the second / character like in
collections/shirts/products/tee-shirt,
collections(/(?!/|shirts|tea|[\w]+/products)) seems to work.

commented: Perfect! Just what I was looking for. +9

Of course if you're not coding in VBasic like me, most probably you'll have to escape the slash /. So the pattern becomes collections\/(?!\/|shirts|tea|[\w]+\/products)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.