injoy
injoy

Reputation: 4383

What's the difference between * and .* in regular expressions?

I saw the wildcard of * been used in some command like: find *.jpg, which means find any files ended with .jpg. However, in regular expressions, .* also means that 0 or more times for any character. So, what's the difference between them? When is * been used and when is .*?

Upvotes: 2

Views: 2798

Answers (2)

abiessu
abiessu

Reputation: 1927

In regular expressions, * is the "zero or more" repetition marker. . is the "any single character (with caveats)" expression.

In a find the * character is an "anything" wildcard and is not really a regular expression character. The ? is the "any single character" in search situations of this kind.

Upvotes: 5

Keith Thompson
Keith Thompson

Reputation: 263627

Filename matching patterns (or globs) and regular expressions are two similar but different things.

For both of them, most characters simply match themselves, and certain special characters have special meanings which can be suppressed by preceding them with a backslash \.

In a filename matching pattern (as used by shells and the find command), ? matches any single character and * matches zero or more arbitrary characters. A . (period) is not a special character; it just matches a literal . character.

In a regular expression (as used by grep, many text editors, and most commands that perform searches on text), . matches any single character and * denotes zero or more occurrences of whatever precedes it.

There's quite a bit more to it than that. Filename matching patterns have special rules for "hidden" files whose names start with ., and the syntax and rules can vary somewhat from one shell to another. Regular expressions also exist in several forms (grep vs. egrep, vs. Perl or PCRE, for example).

Upvotes: 5

Related Questions