in reply to Re: search for 'cat' not followed by 'dog'
in thread search for 'cat' not followed by 'dog'

# /cat(?:(?!\s*dog))/

I don't understand why you wrap  (?!\s*dog) in a non-capturing group.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: search for 'cat' not followed by 'dog'
by stevieb (Canon) on Jan 28, 2019 at 20:31 UTC

    Old habit that I write in a non-capture when I'm doing grouping. In this case, after some *quick* testing, I realize that the negative lookahead doesn't appear to capture by default. In fact, even with benchmarking, the non-cap is effectively no more efficient than leaving it out.

      Note that even if the lookarounds did capture, putting a non-capturing group around a capturing group doesn't stop it from capturing, i.e. "x"=~/(?:(x))/ still puts "x" in $1 (you can even do funky stuff like "xyz"=~/(?=(y))/ which will capture "y" without advancing the match position past it).

      As of 5.22 you can say (?n:...) and any capture groups inside will be non-capturing.