http://www.perlmonks.org?node_id=588315

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
A few nights ago on #perl (freenode), someone asked how to write a regex that matched everything but /$some_regex/. The obvious solution of $thing !~ /$some_regex/; was given. The person then explained that this was for WWW::Mechanize find_all_links() method. The interface looks like:
$mech->find_all_links(text_regex => qr/download/i);
This can be worked around without needing to negate the regex. Just post process all links with !~ yourself. Another alternative would be to propose a patch to Andy. In any case, it got me to thinking about how to negate regexes. I know that it is rare that you might have to do this, but it seems like it might be useful knowledge if for no other reason then to understand how regexes work better.

Originally, I was planning on writing a tutorial. Then I remembered that I stink at regexes and realized that even simple regexes can be hard to negate:

/\d/ # contains a digit /^\D*$/ # doesn't contain a digit /[abc]/ # contains either the letter a, b, or c /^[^abc]*$/ # doesn't contain a, b, or c /foo|bar/ # contains foo or bar ??? # doesn't contain foo or bar

So what tools, tips, and tricks of the trade do you have to share for these rare occassions you need to write a regex that matches everything another regex doesn't?

Cheers - L~R