Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Negating Regexes: Tips, Tools, And Tricks Of The Trade

by Sartak (Hermit)
on Dec 09, 2006 at 02:08 UTC ( #588749=note: print w/replies, xml ) Need Help??

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 (Pope) 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 :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588749]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2016-10-24 11:35 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (305 votes). Check out past polls.