Glob (Wildcard)

> Procedural Languages > Multilingual Regular Expression Syntax (Pattern)

1 - 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.

Advertising

3 - Syntax

  • An asterisk, *, matches any number of characters (including none).
  • Two asterisks, **, works like * but crosses directory boundaries (ie complete paths)
  • A question mark, ?, matches exactly one character.
  • 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”.
  • Square brackets convey a set of single characters or, when the hyphen character (-) is used, a range of characters. For example:
    • [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.
    • Within the square brackets, *, ?, and \ match themselves.
  • All other characters match themselves.
Advertising

3.1 - 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.

4 - Example

  • *.html – Matches all strings that end in .html
  • ??? – Matches all strings with exactly three letters or digits
  • *[0-9]* – Matches all strings containing a numeric value
  • *.{htm,html,pdf} – Matches any string ending with .htm, .html or .pdf
  • a?*.java – Matches any string beginning with a, followed by at least one letter or digit, and ending with .java
  • {foo*,*[0-9]*} – Matches any string beginning with foo or any string containing a numeric value
Advertising

5 - Library

6 - Documentation / Reference