in reply to regex, use of lookaround for whole word filtering

Simple:

!/neither.*nor/ && /nor/

Complicated (and surely slower):

/^(?:(?!neither).)*nor/

Update: Oops, they're not quite equivalent.
The first will match when no "neither" is found before the last "nor".
The second will match when no "neither" is found before the first "nor".

Replies are listed 'Best First'.
Re^2: regex, use of lookaround for whole word filtering
by aquarium (Curate) on Aug 11, 2010 at 03:35 UTC
    You could probably make the first regex use a non-greedy .*
    I think that's the simpler approach rather than negative look behind and what not. The same can be applied to many other programming logic problems..useful sometimes to think about chunks of code logic or regex patterns as sets in finite space.
    the hardest line to type correctly is: stty erase ^H

      You could probably make the first regex use a non-greedy .*

      Wouldn't help.

      >perl -E"$_='nor neither nor'; say !/neither.*nor/ && /nor/ ?1:0" 0 >perl -E"$_='nor neither nor'; say !/neither.*?nor/ && /nor/ ?1:0" 0

      Non-greediness is very fragile and frequently misused.

        Two regexes with capturing in the first one, then:
        $test = "a nor b neither"; say $test =~ /(.*?nor)/i && $1 !~ /neither/i ? 1 : 0;