.net – Regex for string not containing multiple specific words

netregex

I'm trying to put together a regex to find when specific words don't exist in a string. Specifically, I want to know when "trunk", "tags" or "branches" doesn't exist (this is for a Subversion pre-commit hook). Based on the Regular expression to match string not containing a word answer I can easily do this for one word using negative look-arounds:

^((?!trunk).)*$

It's the "and" operator I'm struggling with and I can't seem to get combinations including the other two words working.

This is running fine in .NET with a single word:

var exp = new Regex(@"^((?!trunk).)*$");
exp.IsMatch("trunk/blah/blah");

It will return false as it currently stands or true if "trunk" doesn't exist in the path on the second line.

What am I missing here?

Best Answer

Use a negative look-ahead that asserts the absence of any of the three words somewhere in the input:

^(?!.*(trunk|tags|branches)).*$

I also slightly rearranged your regex to correct minor errors.