Regular Expression Opposite

Couldn't you just check to see if there are no matches? I don't know what language you are using, but how about this pseudocode?

if (!'Some String'.match(someRegularExpression))
    // do something...

If you can only change the regex, then the one you got from your link should work:

/^((?!REGULAR_EXPRESSION_HERE).)*$/

The reason your inverted regex isn't working is because of the '^' inside the negative lookahead:

/^((?!^[ab].).)*$/
      ^            # WRONG

Maybe it's different in vim, but in every regex flavor I'm familiar with, the caret matches the beginning of the string (or the beginning of a line in multiline mode). But I think that was just a typo in the blog entry.

You also need to take into account the semantics of the regex tool you're using. For example, in Perl, this is true:

"abc" =~ /[ab]./

But in Java, this isn't:

"abc".matches("[ab].")

That's because the regex passed to the matches() method is implicitly anchored at both ends (i.e., /^[ab].$/).

Taking the more common, Perl semantics, /[ab]./ means the target string contains a sequence consisting of an 'a' or 'b' followed by at least one (non-line separator) character. In other words, at ANY point, the condition is TRUE. The inverse of that statement is, at EVERY point the condition is FALSE. That means, before you consume each character, you perform a negative lookahead to confirm that the character isn't the beginning of a matching sequence:

(?![ab].).

And you have to examine every character, so the regex has to be anchored at both ends:

/^(?:(?![ab].).)*$/

That's the general idea, but I don't think it's possible to invert every regex--not when the original regexes can include positive and negative lookarounds, reluctant and possessive quantifiers, and who-knows-what.


You can invert the character set by writing a ^ at the start ([^…]). So the opposite expression of [ab] (match either a or b) is [^ab] (match neither a nor b).

But the more complex your expression gets, the more complex is the complementary expression too. An example:

You want to match the literal foo. An expression, that does match anything else but a string that contains foo would have to match either

  1. any string that’s shorter than foo (^.{0,2}$), or
  2. any three characters long string that’s not foo (^([^f]..|f[^o].|fo[^o])$), or
  3. any longer string that does not contain foo.

All together this may work:

^[^fo]*(f+($|[^o]|o($|[^fo]*)))*$

But note: This does only apply to foo.