gpt4 book ai didi

regex - 在 vim 的正则表达式引擎中,为什么有些元字符会被转义而有些则不会?

转载 作者:行者123 更新时间:2023-12-04 13:05:20 25 4
gpt4 key购买 nike

为什么你必须在他们的正则表达式引擎中转义一些元字符,而不是其他?例如:

/foo[1-9]*  

按预期工作,但正则表达式
foo[1-9]+  

必须表示为
/foo[1-9]\+  

在 vim 中。有人知道吗?

最佳答案

这是因为 vim(实际上是 vi )早在 perl 之前就创建了自己的正则表达式风格。甚至 POSIX BRE 和 ERE 也出现在 vim 之后 wikipedia .他们仍然保持着自己的 flavor ,所以这是完全不同的。
为了使这里的答案更足智多谋,引自 ed 's wiki .

The editor was originally written in PDP-11/20 assembler in 1971 by Ken Thompson. Many features of ed came from the qed from his alma mater University of California at Berkeley3 Thompson was very familiar with qed, and had reimplemented it on the CTSS and Multics systems. His versions of qed were the first to implement regular expressions. Although regular expressions are part of ed, their implementation is considerably less general than that in qed.

Aspects of ed went on to influence ex, which in turn spawned vi. The non-interactive Unix command grep was inspired by a common special uses of qed and later ed, where the command g/re/p means globally search for the regular expression re and print the lines containing it. The Unix stream editor, sed implemented many of the scripting features of qed that were not supported by ed on Unix. In turn sed influenced the design of the programming language AWK - which inspired aspects of Perl.


这两段信息量很大!我希望我能把这一切都大胆起来。一些亮点,
  • 肯·汤普森写道 ed 1971 年。ed实际上是对 qed 的重新实现.
  • Ken 在他的 qed 版本中添加了正则表达式这实际上是 ed .
  • 灵感来自 ed , 1976 年威廉·乔伊 (Bill Joy) 写道 ex wikipedia
  • Bill Joy 在 1976 年写道 vi作为名为 ex 的行编辑器的可视模式wikipedia
  • grep灵感来自 qed 的特殊用途及以后 ed .
  • sed实现了许多在 Unix 上不受 ed 支持的 qed 脚本功能
  • sed影响了 awk 的设计.

  • 所以vi正则表达式在 ed中写于 1971 年。它早于任何其他正则表达式风格。

    关于regex - 在 vim 的正则表达式引擎中,为什么有些元字符会被转义而有些则不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998913/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com