A glob pattern matching library for Motoko that supports a wide range of pattern matching features.
mops install glob
To set up MOPS package manager, follow the instructions from the MOPS Site
import Glob "mo:glob";
// Basic matching
Glob.match("file.txt", "*.txt"); // true
Glob.match("dir/file.txt", "**/*.txt"); // true
Glob.match("script.js", "*.txt"); // false
// Directory matching
Glob.match("/foo/bar/baz", "/foo/*/baz"); // true
Glob.match("/foo/bar/qux/baz", "/foo/**/baz"); // true
*
matches any number of characters within a path segment?
matches exactly one character
Glob.match("hello.txt", "h*.txt"); // true
Glob.match("hello.txt", "h????.txt"); // true
*/
matches a single directory level**/
matches zero or more directory levels
Glob.match("/a/b/c", "/a/*/c"); // true
Glob.match("/a/b/c/d", "/a/**/d"); // true
[abc]
matches any one character listed[a-z]
matches any one character in the range[!abc]
matches any one character not listed
Glob.match("file1.txt", "file[1-3].txt"); // true
Glob.match("fileA.txt", "file[ABC].txt"); // true
Glob.match("fileD.txt", "file[!ABC].txt"); // true
!pattern
matches paths that don't match the pattern
Glob.match("/static/public/file.js", "!/static/private/**"); // true
Glob.match("/static/private/file.js", "!/static/private/**"); // false
- Dot files (hidden files) are included in wildcard matches by default
- Path separator normalization is handled automatically
- Supports both absolute and relative paths
- Escaping special characters with backslash
Matches a path against a glob pattern and returns true if it matches.
Parameters:
path
: The path to testpattern
: The glob pattern to match against
Returns:
Bool
: True if the path matches the pattern, false otherwise
// Match specific file types
Glob.match("/static/file.js", "/static/*.js"); // true
Glob.match("/static/file.min.js", "/static/*.js"); // true
// Match files in nested directories
Glob.match("/static/css/file.css", "/static/**/*.css"); // true
Glob.match("/static/css/nested/file.css", "/static/**/*.css"); // true
// Version directories with multiple extensions
Glob.match("/static/v1.2.3/app.js", "/static/v[0-9]*\\.[0-9]*\\.[0-9]*/**/*.js"); // true
// Multiple file extensions
Glob.match("/static/styles.min.css", "/static/**/*.min.{js,css}"); // true