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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.