in reply to Negating Regexes: Tips, Tools, And Tricks Of The Trade

It's interesting that you ask this now. Just yesterday I was developing an IRC bot and a question like this came up; basically, how to write the negation of a regex.

The way I solved it was to add a new flag (not to Perl, but to the regex the users write), /r, that negates the return value of the match. So a user can do !grep /^ascended$/r to match all entries that don't match exactly "ascension". I haven't run into any major problems yet. :) I only match in boolean context (and without using any capturing variables).. I don't know if it'd work so well if I needed to do anything more complicated.

Replies are listed 'Best First'.
Re^2: Negating Regexes: Tips, Tools, And Tricks Of The Trade
by ikegami (Patriarch) on Dec 09, 2006 at 05:53 UTC

    How's grep /^ascended$/r different from grep !/^ascended$/?

    Update: Nevermind, I understand. The user submits /^ascended$/r, which you transform to !/^ascended$/ to execute. I thought you had patched Perl :)