You have a negative approval position back.
This is zero width, so it yshould be before you wrote it.
Given:
$ echo $'xy\nay\nyx'
xy
ay
yx
The lookbehind image /y(?<!x)/matches the lines with xfront or back ybecause it yis behind this statement (not x):
$ echo $'xy\nay\nyx' | perl -n -e 'print if /y(?<!x)/'
xy
ay
yx
Note that it yxalso matches the statement preceding xand looks at y, so all three lines match.
What are you looking for:
$ echo $'xy\nay\nyx' | perl -n -e 'print if /(?<!x)y/'
ay
yx
Demo
Further explanation.
Or you need to consider the width y(or whatever it matches) if you look in the opposite direction after y, including in the statement y:
$ echo $'xy\nay\nyx' | perl -n -e 'print if /y(?<!xy)/'
ay
yx
source
share