Why is it counter intuitive?
/(?<!\d)\d{8}(?!\d)/
,here (?<!\d)
comes first,but called lookbehind,(?!\d)
next,but called lookahead.All are counter intuitive.
What's the reason to name it this way?
Why is it counter intuitive?
/(?<!\d)\d{8}(?!\d)/
,here (?<!\d)
comes first,but called lookbehind,(?!\d)
next,but called lookahead.All are counter intuitive.
What's the reason to name it this way?
Because the regex engine consumes characters from start to end. So the "ahead" is towards the end of the string, and the "behind" is towards the start.
...xyz12345678abc...
behind --->---------- ahead
The (?!\d)
is the assertion that there is a decimal number that comes after the \d{8}
, so the regex engine needs to check the characters in the direction to the end of the string, i.e. look-ahead.
Similar for look-behind.
They are named based on what they do, not how you happen to use it in that specific expression.
The lookbehind is looking for a match in the string behind (to the left of) the current position.
The lookahead is looking for a match in the string ahead of (to the right of) the current position.
It's simple, really.
ab
Now read the characters left to right.
a
is behind b
;
b
is ahead of a
That said, you are right that these kinds of relativism can be confusing. For example, in Aymaran culture, the future is behind; the past is ahead.
It's counterintuitive because there is no consensus whether time goes from front to back or back to front and you simply has a different mindset.
In English we say "Leave the past behind", yet "past" is something that happens "before" (fore = front).