Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between glob-style pattern and regular expression?

Tags:

regex

glob

I've encountered situations where only glob-style patterns are supported and full regular expression support is not there, for instance redis keys. I'd like to know the distinction between the two. Also it makes me wonder, is implementing regex matching algorithm way harder compared to glob-style pattern that some softwares doesn't support regex at all?

like image 323
aamir Avatar asked May 16 '14 18:05

aamir


People also ask

What is the difference between glob and regular expression?

Regular expressions are used in commands / functions for pattern matching in text. For example in the pattern parameter of grep , or in programming languages. File name globbing is used by shells for matching file and directory names using wildcards. The capabilities of globbing depend on the shell.

What is the difference between regex and pattern?

Pattern matching is used by the shell commands such as the ls command, whereas regular expressions are used to search for strings of text in a file by using commands, such as the grep command.

Does glob use regex?

The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported.

What is meant by glob patterns?

Globs, also known as glob patterns are patterns that can expand a wildcard pattern into a list of pathnames that match the given pattern. On the early versions of Linux, the command interpreters relied on a program that expanded these characters into unquoted arguments to a command: /etc/glob .


1 Answers

Traditional glob wildcards support a very narrow set of metacharacters — * is "anything", ? is an arbitrary single character; Bourne shell also supports [a-z123] for a single character out of a set of alternatives, and [!x-z789] any one except those listed.

Regex obviously is much richer, supporting repetitions, and (in ERE) alternation and specific numbers of repetitions. Perl-style regex further extends the formalism to the point where multiple books have been written, and more will be.

Basic regex is not altogether a lot more challenging to program than glob wildcards, and these days, a competent programmer would link to an existing library in either case, anyway.

Many simpler systems don't want to burden their users with the complexity of learning regular expressions — even basic wildcards are a challenge to explain to your average sales guy^W^W person who isn't a full-time computer user.

like image 123
tripleee Avatar answered Sep 17 '22 17:09

tripleee