The glob_match function is the primary API for checking if a string matches a given glob pattern. It supports standard globbing features including wildcards, character sets, and directory traversal.
Supported Syntax
*: Matches any number of characters within a single path segment (does not cross / boundaries unless part of **).**: Matches any number of characters, including directory separators (recursive globbing).?: Matches exactly one character.[a-z]: Matches a single character within a specified range or set.[^a-z]: Matches a single character NOT within the specified range or set.{a,b}: Matches one of the comma-separated alternatives.!: Used for negation (e.g., !*.md matches anything that is NOT a .md file).[]: Used for character sets, including escaping characters like \] or \- to include them in the set.
Note that * does not match across directory separators, while ** is designed for recursive matching across multiple directory levels.
// Example: Basic matching
assert!(glob_match("a?c", "abc"));
assert!(glob_match("*.js", "z.js"));
// Example: Recursive matching with **
assert!(glob_match("a/**/*.txt", "a/x/y/z.txt"));
assert!(glob_match("**/*.js", "a/b/c.js"));
// Example: Character sets and negation
assert!(glob_match("[^a-c]*", "d"));
assert!(glob_match("t[a-g]n", "ten"));
assert!(!glob_match("!*.md", "foo.md"));
// Example: Braces for alternatives
assert!(glob_match("{a,b,c}", "b"));
assert!(glob_match("a/{a,b}", "a/b"));