0

I need a regex that will accept all paths except those that have /cantbe or /cantbe/this

So

this/is/ok/filename.tr

is a match but

this/is/ok/cantbe/filename.tr

or

this/is/ok/cantbe/this/filename.tr

are not matches.

I tried

.*(?!\/cant\/)(?!\/cant\/this).*\.tr

but the paths above are still matches

user2175783
  • 911
  • 1
  • 9
  • 20
  • 1
    Would `is/this/ok/cantbesomething` be a match? And since I assume `/it/cantbe/that` shouldn't be a match, since it contains `/cantbe`, why do you have the extra rule that `/cantbe/this` cannot be in it, since that already contains `/cantbe`? – Grismar Oct 08 '20 at 00:33
  • @Grismar `is/this/ok/cantbesomething` would not be a match. You are right that I can drop `/cantbe/this` since it is included in `/cantbe/`. Basically any path with `/cantbe/` is not a match. – user2175783 Oct 08 '20 at 00:36
  • The `.*` should be inside the lookahead, and use an anchor `^` to assert the start of the string `^(?!.*\/cantbe).+` See https://regex101.com/r/9nGZfx/1 – The fourth bird Oct 08 '20 at 12:33

1 Answers1

1

try this

^((?!\/cantbe\b).)*$

Try it out here

This is explained quite well on this question Regular expression to match a line that doesn't contain a word

vedda
  • 46
  • 4