Regex — Remember Lookbehind Sun and Lookahead Moon
How to remember lookbehind and lookahead regular expression.
Now I show some pictures for my expression.
There is regular expression
You know caret means start
Time advances from day to night
Because regex match advances from left to right means start to end.
You know regex lookahead and lookbehind?
I will express by same way about 4.
Let's be friend with mysterious marks
Umm take a Time second time
Behind means like Start and Head means like End
Time advances in same world
In the first place we can ignore question mark
Because it is common mark of starting special regex syntax.
"Positive Lookbehind" has "An toward past" and says "Yes".
"Negative Lookbehind" has a "An toward past" and says "No".
"Positive Lookahead" has no mark about time advance
But now imagine omitted mark toward Head.
Because time advances toward head is very natural.
So we do not have to express about it.
"Pegative Lookahead" has omitted mark toward head and says "Yes".
"Negative Lookahead" has omitted mark toward head and says "No".
Replace "B" with "X" in "ABC".
$ echo " ABC" | perl -pe ' s/(?<=A)B/X/g'
AXC
$ echo " ABC" | perl -pe ' s/(?<!A)B/X/g'
ABC
$ echo " ABC" | perl -pe ' s/B(?=C)/X/g'
AXC
$ echo " ABC" | perl -pe ' s/B(?!C)/X/g'
ABC
bash-4.4
perl 5, version 28