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\" does
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.
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