Q:

Regex: Allow a string that contains "foo" but not if that same string also contains "bar"

My input is a filename, some examples:
/some/file/path/thefooisgood.txt
/some/file/path/thisfooisalsogood.txt
/some/file/path/thisfooalsohasbar.txt

I want to find/allow all filenames/paths that contain the substring foo so long as the string doesn't also have the substring bar in it.
Substring bar may appear anywhere in the file path, doesn't always necessarily come after foo and may sometimes be in the path name, not just the filename. Same goes for foo. I can't seem to get it right.

A:

You may use a negative lookahead based regex as this:
^(?!.*bar).*foo

RegEx Demo
Details:

(?!.*bar): Assert that we don't have bar anywhere in input
.*foo: Match foo anywhere in input

