Confusion with shell-globbing wildcards and Regex

徘徊边缘 提交于 2019-12-21 20:55:07

问题


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?

回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1323805/confusion-with-shell-globbing-wildcards-and-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!