Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negating a set of words via java regex

Tags:

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.

like image 626
Abhishek Avatar asked Aug 26 '09 09:08

Abhishek


People also ask

How do I negate a string in regex?

Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.

How do you negate a regular expression in Java?

Negation: “[^]” It defines the symbol as the negation variant of the character class. It matches all the characters that are not specified in the character class in regex in java. (eg) (i).

What does \b mean in regex Java?

In Java, "\b" is a back-space character (char 0x08 ), which when used in a regex will match a back-space literal.

How do you end a regex match?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.


1 Answers

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?

like image 130
Kamarey Avatar answered Oct 02 '22 11:10

Kamarey