正则表达式、lookahead 和lookbehind 的命名约定
为什么它违反直觉?
/(?,这里
(?先出现,但称为lookbehind,
(?!\d)
next,但称为lookahead。一切都是反直觉的。
这么命名的理由是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为正则表达式引擎从头到尾都会消耗字符。因此,“前面”是朝向字符串的末尾,“后面”是朝向字符串的开头。
(?!\d)
断言\d{8}
后面有一个十进制数,因此正则表达式引擎需要检查中的字符到字符串末尾的方向,即向前查看。类似于后视。
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.
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.
这是违反直觉的,因为时间是从前到后还是从后到前并没有达成共识,而你只是有不同的心态。
在英语中,我们说“把过去抛在后面”,但“过去”是发生在“之前”的事情(fore = front)。
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).
这很简单,真的。
现在从左到右阅读这些字符。
a
位于b
后面;b
领先于a
也就是说,你是对的,这种相对主义可能会令人困惑。例如,在艾马拉文化中,未来已然过去;过去的事情就在前面。
It's simple, really.
Now read the characters left to right.
a
is behindb
;b
is ahead ofa
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.