Glob - Globbing (Wildcard expression)

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.

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

3.1 - Asterix

An asterisk (also known as star) , *, matches any number of characters (including none).

Example:

  • *.html – Matches all strings that end in .html

3.2 - 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


**/

3.3 - 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

3.4 - 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

3.5 - 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

3.6 - 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 - 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

5 - Language

globbing in other languages

5.1 - Bash

5.2 - Node

5.3 - Java

6 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap