how to negate any regular expression in Java
Asked Answered
H

3

18

I have a regular expression which I want to negate, e.g.

/(.{0,4})

which String.matches returns the following

"/1234" true
"/12" true
"/" true
"" false
"1234" false
"/12345" false

Is there a way to negate (using regx only) to the above so that the results are:

"/1234" false
"/12" false
"/" false
"" true
"1234" true
"/12345" true

I'm looking for a general solution that would work for any regx without re-writing the whole regex.

I have looked at the following How to negate the whole regex? using (?! pattern), but that doesn't seem to work for me.

The following regx

(?!/(.{0,4}))

returns the following:

"/1234" false
"/12" false
"/" false
"" true
"1234" false
"/12345" false

which is not what I want. Any help would be appreciated.

Hiedihiemal answered 22/12, 2011 at 23:12 Comment(2)
Couldn't you just match and negate whether or not it matched? That's the general-purpose solution for any regex w/o rewriting the regex.Praefect
This matching coding is part of an existing framework which cannot be changed without breaking other things.Hiedihiemal
S
40

You need to add anchors. The original regex (minus the unneeded parentheses):

/.{0,4}

...matches a string that contains a slash followed by zero to four more characters. But, because you're using the matches() method it's automatically anchored, as if it were really:

^/.{0,4}$

To achieve the inverse of that, you can't rely on automatic anchoring; you have to make at least the end anchor explicit within the lookahead. You also have to "pad" the regex with a .* because matches() requires the regex to consume the whole string:

(?!/.{0,4}$).*

But I recommend that you explicitly anchor the whole regex, like so:

^(?!/.{0,4}$).*$

It does no harm, and it makes your intention perfectly clear, especially to people who learned regexes from other flavors like Perl or JavaScript. The automatic anchoring of the matches() method is highly unusual.

Superhuman answered 23/12, 2011 at 0:9 Comment(3)
So will this work for any regex? i.e. String negateRegex(String regex) { return "(?!" + regex + "$).*"; } would work as expected? Can this negate itself? i.e. would negateRegex(negateRegex(regex)) be functionally the same as regex?Gloze
Hmm, I've done some preliminary testing here[1] and it does seem to be working in all the cases I've tried, including negating a negation. [1] regexplanet.com/advanced/java/index.htmlGloze
I don't believe you can manipulate regex like string. Do you consider \Q and \E? Do you consider backslash at the end?Darwinism
D
9

I know this is a really old question but hopefully my answer can help anyone looking for this in the future.

While Alan Moore's answer is almost correct. You would need to group the whole regex too, or else you risk anchoring only part of the original regex.

For example if you want to negate the following regex: abc|def (which matches either "abc" or "def"

Prepending (?! and appending $).*. You will end up with (?!abc|def$).*.

The anchor here is only applying to def, meaning that "abcx" will not match when it should.

I would rather prepend (?!(?:and append )$).*.

String negateRegex(String regex) {
    return "(?!(?:" + regex + ")$).*";
}

From my testing it looks like negateRegex(negateRegex(regex)) would indeed be functionally the same as regex.

Dabchick answered 21/8, 2020 at 19:49 Comment(0)
B
0

Assuming our regex is MYREG, match other lines with:

^(?:(?!.*MYREG).*)$
Baese answered 7/4, 2022 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.