与 shell 通配符和正则表达式的混淆

发布于 2024-08-02 23:09:34 字数 254 浏览 2 评论 0 原文

回复发起。

  1. 如何仅使用正则表达式而不使用通配符?
  2. 如果可以使用正则表达式,哪里真正需要通配符和通配符?
  3. 正则表达式是从通配符演变而来还是反之亦然?

Initiated by the reply.

  1. How can I use only Regex instead of wildcards?
  2. Where do you really need wildcards and globbing if you can use Regex?
  3. Have Regexes evolved from wildcards or vice versa?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

提赋 2024-08-09 23:09:34

我认为您的困惑是基于 shell 通配符( * 字符)和正则表达式符号(* 字符)之间的差异。正则表达式不是 shell-globbing,它们更加强大和有用,但是对于日常 shell 使用,通配符和 shell-globbing 已经“足够好了”。

  1. 如何仅使用正则表达式而不使用通配符?

不要使用外壳。编写一个 Perl/Python/Ruby/[此处您选择的脚本语言] 脚本来为您完成这项工作。它可能会更快,因为它不需要分叉那么多。

  • 如果可以使用正则表达式,哪里真正需要通配符和通配符?
  • 不。但在大多数 shell 中,您没有正则表达式,因此您有 glob。将它们视为穷人的正则表达式。

  • 正则表达式是从通配符演变而来的还是反之亦然?
  • 正则表达式来自集合论,特别是早期的文本编辑器(一种名为 ed 的早期 Unix 文本编辑器具有类似正则表达式的功能,然后在一个名为 grep 的小程序中重新使用了该功能) code>,您可能听说过)。我想通配符只是 shell 的功能。它们并不难实现,因此 shell 编写者会相当快地添加它们,而且开销很小。

    I think your confusion is based on the differences between shell-globbing wildcards (the * character) and the regular expression symbol (the * character). Regexes are not shell-globbing, they are a lot more powerful and useful, but for everyday shell use, wildcards and shell-globbing are "good enough."

    1. How can I use only Regex instead of wildcards?

    Don't use the shell. Write a Perl/Python/Ruby/[your-choice-of-scripting-language-here] script to do the job for you. It'll probably be faster, since it won't have to fork so much.

    1. Where do you really need wildcards and globbing if you can use Regex?

    No. But in most shells, you don't have regexes, so you have globs. Think of them as a poor-man's regex.

    1. Have Regexes evolved from wildcards or vice versa?

    Regexes came from set theory, and specifically early text editors (one early Unix text editor called ed had a regex-like feature, which was then re-used in a little program called grep, which you might have heard of). I imagine wildcards have just been features of the shell. They can't be hard to implement, so shell writers would add them fairly quickly, and with little overhead.

    与往事干杯 2024-08-09 23:09:34

    手册页中描述:

    -名称模式

    如果正在检查的路径名的最后一个组成部分与模式匹配,则为 true。特殊的 shell 模式匹配字符([]*?)可以用作模式的一部分。这些字符可以通过使用反斜杠 (\) 转义来显式匹配。

    换句话说,在 shell glob 模式中可用的模式也可以由 find 使用。

    手册页通常可以告诉您很多信息。 ;)

    $ man find
    

    了解更多信息。

    Described in the man page:

    -name pattern

    True if the last component of the pathname being examined matches pattern. Special shell pattern matching characters ([, ], *, and ?) may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (\).

    So in other words, patterns that are usable in shell glob patterns are usable by find.

    Man pages can generally tell you a lot. ;)

    $ man find
    

    for more information.

    春夜浅 2024-08-09 23:09:34

    我最初的问题有一个错误的前提;它们是通配符,而不是正则表达式! Glob 程序处理通配符。

    正则表达式

    请注意
    通配符模式不规则
    表达方式,虽然有点
    相似的。首先,他们匹配
    文件名,而不是文本,以及
    其次,公约并不是
    相同:例如,在常规中
    表达式“*”表示零个或多个
    前面事物的副本。现在
    正则表达式有括号
    否定的表达式
    由 '^' 表示,POSIX 已声明
    通配符模式的效果
    “[^...]”未定义。

    解释并不是 100% 彻底。例如,您可以轻松地使用正则表达式匹配文件名。

    My initial question had a wrong premise; they are wildcards, not regexes! Glob-program handles wildcards.

    Regular expressions

    Note that
    wildcard patterns are not regular
    expressions, although they are a bit
    similar. First of all, they match
    filenames, rather than text, and
    secondly, the conventions are not the
    same: for example, in a regular
    expression '*' means zero or more
    copies of the preceding thing. Now
    that regular expressions have bracket
    expressions where the negation is
    indicated by a '^', POSIX has declared
    the effect of a wildcard pattern
    "[^...]" to be undefined.

    The explanation is not 100% thorough. For example, you can easily match filenames with Regex.

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