Harry Ohlsen wrote:

> A colleague of mine just asked me whether it was possible to invert an
> arbitrary regular expression.  Ie, create a new regular expression that
> matches whatever the original *didn't*.
> 
> However, I think it would be much harder for arbitrary cases.  Eg, how
> would one automatically invert something like the following
> 
>    /<[^<>]*id="!?(region8-).*?>/
> 
> Note that I'm not talking about finding lines that don't contain the
> pattern (a la "grep -v"); I'm talking about finding all the occurrences
> within a single line that don't match it.

I think the most general way of doing this is this:

   /(?!#{re}).{#{match_length}}/

But that will of course only work for constant-length expressions. 
Variable, but finite length sets can be done through alternation, but 
that would not be enough to work for the above.

It can also be quite hard to find the match length of an arbitrary 
Regexp so this *is* fairly esoteric stuff.

I have needed this before to generally match Strings that are delimited 
by some sort of String which can also be escaped by another String -- 
the classic example is 'foo\'bar', but you might as well decide to do 
DELIMtexttextESCAPEDELIMtextDELIM. Of course the match length is always 
known in this case because the delimiter is going to be a String.

This also won't produce overlapping matches when used in .scan, but that 
is usually the case for Regexps.