Regex for string not containing multiple specific words

.NetRegex

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

Use a "standard" match and look for !IsMatch

var exp = new Regex(@"trunk|tags|branches");
var result = !exp.IsMatch("trunk/blah/blah");

Why persons love to make their life difficult?

Ah... And remember the ass principle! http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html

So it would be better to write

var exp = new Regex(@"\b(trunk|tags|branches)\b");

But if you really need a negative lookahed expression, and keeping up with the ass principle

var exp = new Regex(@"^(?!.*\b(trunk|tags|branches)\b)";

Tester: http://gskinner.com/RegExr/?2uv1g

I'll note that if you are looking for full paths (words separated by / or \) then

var exp = new Regex(@"^(?!.*(^|\\|/)(trunk|tags|branches)(/|\\|$))";

Tester: http://gskinner.com/RegExr/?2uv1p

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTroy HuntView Question on Stackoverflow
Solution 1 - .NetBohemianView Answer on Stackoverflow
Solution 2 - .NetxanatosView Answer on Stackoverflow