|We don't bite newbies here... much|
Negating Regexes: Tips, Tools, And Tricks Of The Tradeby Limbic~Region (Chancellor)
|on Dec 07, 2006 at 13:47 UTC||Need Help??|
Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
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:
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:
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