To find, this not preceded by that, use negative look-behind with negative look-ahead , use (?<!pattern) with (?!pattern), use (?<!pattern)(?!pattern)

After studying perlre, I come up with a solution   s/((?<!bar)foo)/\U$1/g; or so I think, not noticing the problem output  barfoo bazFOO FOOFOO barfoo

I think great, now to make it generic, so try  s/((?<!bar)\S{3,})/\U$1/g; but it produces  BARFOO BAZFOO FOOFOO BARFOO

Afer much fiddling and reading I try  s/((?<!bar)(?!bar)\S{3,})/\U$1/g; but its off a little, it produces  bARFOO BAZFOO FOOFOO bARFOO

Hmm, eventually I stumble upon a solution  s/\b((?<!bar)(?!bar)\S{3,})/\U$1/g;

This might illuminate some things

#!/usr/bin/perl -- print $_="barfoo bazfoo foofoo barfoo\n"; $snum = s/((?<!bar)foo)/{\U$1}/g; print "$snum $_"; print '#' x 5, "\n"; print $_="barfoo bazfoo foofoo barfoo\n"; $snum = s/((?<!bar)\S{3,})/{\U$1}/g; print "$snum $_"; print '#' x 5, "\n"; print $_="barfoo bazfoo foofoo barfoo\n"; $snum = s/((?<!bar)(?!bar)\S{3,})/{\U$1}/g; print "$snum $_"; print '#' x 5, "\n"; print $_="barfoo bazfoo foofoo barfoo\n"; $snum = s/\b((?<!bar)(?!bar)\S{3,})/{\U$1}/g; print "$snum $_"; print '#' x 5, "\n"; __END__
barfoo bazfoo foofoo barfoo
3 barfoo baz{FOO} {FOO}{FOO} barfoo
#####
barfoo bazfoo foofoo barfoo
4 {BARFOO} {BAZFOO} {FOOFOO} {BARFOO}
#####
barfoo bazfoo foofoo barfoo
4 b{ARFOO} {BAZFOO} {FOOFOO} b{ARFOO}
#####
barfoo bazfoo foofoo barfoo
2 barfoo {BAZFOO} {FOOFOO} barfoo
#####

What YAPE::Regex::Explain says about the winning pattern

use YAPE::Regex::Explain; print YAPE::Regex::Explain ->new( qr/\b((?<!bar)(?!bar)\S{3,})/ )->explain; __END__ The regular expression: (?-imsx:\b((?<!bar)(?!bar)\S{3,})) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \b the boundary between a word char (\w) and something that is not a word char ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- (?<! look behind to see if there is not: ---------------------------------------------------------------------- bar 'bar' ---------------------------------------------------------------------- ) end of look-behind ---------------------------------------------------------------------- (?! look ahead to see if there is not: ---------------------------------------------------------------------- bar 'bar' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- \S{3,} non-whitespace (all but \n, \r, \t, \f, and " ") (at least 3 times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

At one point i tried surrounding with (?>pattern) but it doesn't affect look-arounds, they are "zero-width"

So did I (re)?discover an idiom?

What are alternate ways to write this pattern (note, not  s/\S{3,}/Fixit($1)/ge)?

Other wisdom to learn from this exercise?

Thank you fellow patients

  • Comment on To find, this not preceded by that, use negative look-behind with negative look-ahead , use (?<!pattern) with (?!pattern), (?<!pattern)(?!pattern)
  • Select or Download Code

Replies are listed 'Best First'.
Re: To find, this not preceded by that, use (?<!pattern)(?!pattern) (or not)
by tye (Sage) on Dec 13, 2011 at 15:00 UTC

    The (?<!bar) in your "solution" does nothing. s/\b((?!bar)\S{3,})/\U$1/g behaves the same. The only way for (?<!bar) to matter is due to \b caring about \w vs \W not about \s vs \S. \b\w{3,} would imply (?<!\w) which implies (?<!bar).

    I can't tell what you are actually trying to solve so I don't have much else to offer. Would "foobar" be "\S{3} not preceded by 'bar'"? Is that what you are trying to find?

    - tye        

      Would "foobar" be "\S{3} not preceded by 'bar'"? Is that what you are trying to find?

      Yes it would. I thought this much was clear.

      I see now the wording of (?!pattern) is what confused me

      If you are looking for a "bar" that isn't preceded by a "foo", /(?!foo)bar/ will not do what you want. That's because the (?!foo) is just saying that the next thing cannot be "foo" -- and it's not, it's a "bar", so "foobar" will match. Use look-behind instead (see below).

      And I'm certain now, that I only tried s/((?!bar)\S{3,})/\U$1/g ; without \b

      And here I could have sworn I tried that

      THANKS!