正则表达式、lookahead 和lookbehind 的命名约定

发布于 2024-08-26 16:27:28 字数 257 浏览 16 评论 0原文

为什么它违反直觉?

/(?,这里(?先出现,但称为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦爱 2024-09-02 16:27:28

因为正则表达式引擎从头到尾都会消耗字符。因此,“前面”是朝向字符串的末尾,“后面”是朝向字符串的开头。

    ...xyz12345678abc...
behind --->---------- ahead

(?!\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.

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

峩卟喜欢 2024-09-02 16:27:28

它们是根据它们的作用来命名的,而不是根据您在特定表达方式中碰巧使用它的方式来命名。

后向查找是在当前位置后面(左侧)的字符串中查找匹配项。

前瞻是在当前位置之前(右侧)的字符串中查找匹配项。

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.

找个人就嫁了吧 2024-09-02 16:27:28

这是违反直觉的,因为时间是从前到后还是从后到前并没有达成共识,而你只是有不同的心态。

在英语中,我们说“把过去抛在后面”,但“过去”是发生在“之前”的事情(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).

北座城市 2024-09-02 16:27:28

这很简单,真的。

ab

现在从左到右阅读这些字符。

a 位于 b 后面;
b 领先于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文