Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asterisk(*) and dot-asterisk(.*) in unix regular expression

Tags:

regex

unix

I am always finding myself confused with the usage of these two wildcards in UNIX. I understand asterisk can be used only as a quantifier in a regexp, but UNIX allows * in some cases like the ls command whereas in grep it should be used only as a quantifier. Is there some rule out there which will guide me which one of these to use?

like image 876
toddlermenot Avatar asked Mar 10 '13 12:03

toddlermenot


1 Answers

You're talking about two separate kinds of patterns in UNIX, the glob (wildcard) and the regular expression.

Globs are typically used when matching filenames. Your shell does wildcard expansion automatically on command arguments, which is how you can write

ls *foo*

to list all files containing the string foo. Globs are very simple (ignoring extglob): the syntax typically only supports * (any characters) and ? (any one character). Because of the shell's automatic expansion, these can be used anywhere and must be explicitly suppressed to avoid unwanted matching (e.g. writing grep '.*foo.*' to avoid expanding the * in the pattern).

On the other hand, we have regular expressions, which are used to pattern-match text. Typically, these are used with specific utilities like grep, sed and perl which understand these patterns, though some shells have contexts which take regex (e.g. Bash's ${x/pat/sub}). Regular expression syntax is much more powerful, which makes it ideal for looking through reams of text for specific patterns.

like image 179
nneonneo Avatar answered Sep 18 '22 17:09

nneonneo