Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for string not containing multiple specific words

Tags:

.net

regex

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?

like image 871
Troy Hunt Avatar asked Oct 18 '11 01:10

Troy Hunt


People also ask

How do you regex multiple words?

However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

Is there a regex for anything not a letter?

try doing str. replace(/[^\w]/); It will replace all the non-alphabets and numbers from your string!


2 Answers

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.

like image 157
Bohemian Avatar answered Sep 21 '22 12:09

Bohemian


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

like image 24
xanatos Avatar answered Sep 19 '22 12:09

xanatos