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.
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.
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).
In Java, "\b" is a back-space character (char 0x08 ), which when used in a regex will match a back-space literal.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With