About
Glob is NOT a regular language but is wildly used to define a set of filenames with wildcard characters.
For instance, Bash provide globbing on filenames.
Wildcard / Syntax
The syntax is different by implementation and below are the most known pattern.
- The matchs of the wildcard characters is explained below
- All other characters match themselves.
Asterix
An asterisk (also known as star) , *, matches any number of characters (including none).
Example:
- *.html – Matches all strings that end in .html
Double Asterix
Two asterisks, **, works like * but crosses directory boundaries (ie complete paths) in a file system.
Example:
- The recursive globbing is always located before the last name in the path
src/**/*.c
Syntax and parsing rule: if the two asterisks, ** are followed by a /, this pattern will match only directories and subdirectories. See globstar in bash
**/
Question mark
A question mark, ?, matches exactly one character.
Example:
- ??? – Matches all strings with exactly three letters or digits
- a?*.java – Matches any string beginning with a, followed by at least one letter or digit, and ending with .java
Braces
Braces specify a collection of subpatterns. For example:
- {sun,moon,stars} matches “sun”, “moon”, or “stars”.
- {temp*,tmp*} matches all strings beginning with “temp” or “tmp”.
- *.{htm,html,pdf} – Matches any string ending with .htm, .html or .pdf
- {foo*,*[0-9]*} – Matches any string beginning with foo or any string containing a numeric value
Square brackets
Square brackets convey a set of single characters or, when the hyphen character (-) is used, a range of characters.
- [aeiou] matches any lowercase vowel.
- [0-9] matches any digit.
- [A-Z] matches any uppercase letter.
- [a-z,A-Z] matches any uppercase or lowercase letter.
- [!abc] exclusion does not match the set of characters abc
- Within the square brackets, *, ?, and \ match themselves.
Example:
- *[0-9]* – Matches all strings containing a numeric value
Escape
To match *, ?, or the other special characters, you can escape them by using the backslash character, \. For example:
- \\ matches a single backslash,
- \? matches the question mark.
Note: If you are typing the glob pattern at the console and it contains one of the special characters, you must:
- put the pattern in quotes (“*”),
- use the backslash (\*),
- or use whatever escape mechanism is supported at the command line.
Definition
A possible definition is that a string is a wildcard pattern when it contains one of the characters
- ? to matches any single character
- * to matches any string
- [ for character class definition
- { to define a list of pattern
Language
globbing in other languages