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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |