Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2018-06-21 19:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (119 votes). Check out past polls.