Negating a set of words via java regex

Abhishek picture Abhishek · Aug 26, 2009 · Viewed 53.2k times · Source

I would like to negate a set of words using java regex.

Say, I want to negate cvs, svn, nvs, mvc. I wrote a regex which is ^[(svn|cvs|nvs|mvc)].

Some how that seems not to be working.

Answer

Kamarey picture Kamarey · Aug 26, 2009

Try this:

^(?!.*(svn|cvs|nvs|mvc)).*$

this will match text if it doesn't contain one of svn, cvs, nvs or mvc.

This is a similar question: C# Regex to match a string that doesn't contain a certain string?