与 shell-globbing 通配符和 Regex 的混淆 [英] Confusion with shell-globbing wildcards and Regex

查看:28
本文介绍了与 shell-globbing 通配符和 Regex 的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回复.

  1. 如何只使用正则表达式而不使用通配符?
  2. 如果您可以使用 Regex,您真正需要通配符和通配符的位置是什么?
  3. 正则表达式是从通配符演变而来的,反之亦然?

解决方案

我认为您的困惑是基于 shell 通配符(* 字符)和正则表达式符号(* 字符).正则表达式不是shell-globbing,它们更强大和有用,但对于日常shell使用,通配符和shell-globbing足够好".

<块引用>

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

不要使用外壳.编写一个 Perl/Python/Ruby/[your-choice-of-scripting-language-here] 脚本来为您完成这项工作.它可能会更快,因为它不必分叉太多.

<块引用>

  1. 如果您可以使用 Regex,您真正需要通配符和通配符的位置是什么?

没有.但是在大多数 shell 中,你没有有正则表达式,所以你有 globs.将它们视为穷人的正则表达式.

<块引用>

  1. 正则表达式是从通配符演变而来的,反之亦然?

正则表达式来自集合论,特别是早期的文本编辑器(一个名为 ed 的早期 Unix 文本编辑器具有类似正则表达式的功能,然后在名为 的小程序中重新使用grep,你可能听说过).我想通配符只是 shell 的特性.它们并不难实现,所以 shell 编写者会很快地添加它们,而且开销很小.

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?

解决方案

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.

这篇关于与 shell-globbing 通配符和 Regex 的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆