anon
anon

Reputation:

Confusion with shell-globbing wildcards and Regex

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?

Upvotes: 3

Views: 4127

Answers (3)

Chris Lutz
Chris Lutz

Reputation: 75469

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.

Upvotes: 6

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.

Upvotes: 0

mipadi
mipadi

Reputation: 411260

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.

Upvotes: 5

Related Questions