in reply to Re: Blacklisting with a Regular Expression
in thread Blacklisting with a Regular Expression

/^(?!.*(?:evil|bad|wrong))/
Bingo. This is the correct solution.

I tried this one a little earlier (knowing why it wasn't working):
/^.*(?!evil|bad|wrong)/
...but it never occurred to me to put the .* INSIDE the negative lookahead.

<slaps forehead/>

Thanks to the rest of you for helping, but most of you actually reversed the problem. The regex needed to return a match from the clean string, not from the string containing the blacklisted tokens.

Thanks!!

--Benji

Replies are listed 'Best First'.
Re^3: Blacklisting with a Regular Expression
by hv (Prior) on Aug 19, 2005 at 01:29 UTC

    Just for the sake of completeness, here's an alternative:

    /^(?:(?!evil|bad|wrong).)*\z/

    Eimi's solution is much faster for the problem as given, but this approach gives more control - if your problem becomes any more complex in the future it's a useful trick to have available.

    Hugo