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."
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.
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.
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.
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.
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.
发布评论
评论(3)
我认为您的困惑是基于 shell 通配符(
*
字符)和正则表达式符号(*
字符)之间的差异。正则表达式不是 shell-globbing,它们更加强大和有用,但是对于日常 shell 使用,通配符和 shell-globbing 已经“足够好了”。不要使用外壳。编写一个 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."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.
No. But in most shells, you don't have regexes, so you have globs. Think of them as a poor-man's regex.
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 calledgrep
, 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 glob 模式中可用的模式也可以由
find
使用。手册页通常可以告诉您很多信息。 ;)
了解更多信息。
Described in the man page:
So in other words, patterns that are usable in shell glob patterns are usable by
find
.Man pages can generally tell you a lot. ;)
for more information.
我最初的问题有一个错误的前提;它们是通配符,而不是正则表达式! Glob 程序处理通配符。
解释并不是 100% 彻底。例如,您可以轻松地使用正则表达式匹配文件名。
My initial question had a wrong premise; they are wildcards, not regexes! Glob-program handles wildcards.
The explanation is not 100% thorough. For example, you can easily match filenames with Regex.