views:

94

answers:

4

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?

+2  A: 

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.

KennyTM
Sorry,but this seems doesn't explains away why it has to be like this?
@user: Because you don't walk backwards. (http://en.wikipedia.org/wiki/Backward_running)
KennyTM
But why regex chooses to walk backwards in this case?Is there some benefit?
@user: No it does not (yeah we've different mind sets. Never mind.)
KennyTM
I'm interested in the mind sets.Can you talk more about it? :)
+1  A: 

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.

Guffa
A: 

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.

polygenelubricants
A: 

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).

RichN
This is the most acceptable answer!thanks!
But it's still very odd to name it this way