Regex vs Glob Patterns
Compare regex and glob pattern matching. When to use full regular expressions versus simpler glob wildcards for file paths and pattern matching.
Why This Comparison Matters
Regex and glob patterns both match strings, but they're designed for different contexts. Glob is purpose-built for file path matching โ simple, readable, with just a few wildcard characters. Regex is a general-purpose pattern language with much greater expressive power, but significantly more complex syntax.
- Wildcard: Glob:
*matches anything in a path segment; Regex:.*matches any characters - Directory recursion: Glob:
**matches across directory separators; Regex: no equivalent (must be explicit) - Single character: Glob:
?matches one character; Regex:.matches one character - Character classes: Both support
[abc]and[a-z]syntax - When to use glob: .gitignore rules, file system matching, build tool includes/excludes, shell wildcards
- When to use regex: Input validation, text extraction with capture groups, find-and-replace, log parsing, complex pattern matching
Pattern Syntax Comparison
- Match any chars (same segment): Glob:
*.txt; Regex:[^/]*\.txt - Match across dirs: Glob:
**/*.ts; Regex:.*\.ts - Single char: Glob:
file?.txt; Regex:file.\.txt - Negation: Glob:
!node_modules(tool-dependent); Regex: negative lookahead(?!node_modules) - Anchoring: Glob anchors to path segments automatically; Regex needs
^and$ - Capture groups: Glob: not supported; Regex:
(group1)(group2) - Alternation: Glob:
{ts,tsx}; Regex:(ts|tsx)
Choose the Right Variant
- This page: Regex vs Glob โ syntax and use case comparison
- Regex Tester: Test and debug regular expressions online
Privacy and Data Handling
Regex testing runs entirely in your browser. Your patterns and test input never leave your device.
Frequently Asked Questions
Can I use glob patterns in .gitignore?
Yes โ .gitignore uses glob patterns, not regex. The rules: * matches any file in the current directory, ** matches files in any subdirectory, ? matches a single character, and [abc] matches character classes. A leading / anchors to the repo root. A trailing / matches only directories. Negation with ! un-ignores a previously ignored file. Example: **/__pycache__/ ignores all Python cache directories anywhere in the repo; *.log ignores all log files in the current directory only.
Why is glob faster than regex for file matching?
Glob patterns have limited expressiveness, which allows implementations to match them with simple string operations rather than a full finite automaton. Many file system APIs (like Python's pathlib.glob() or Node's fast-glob library) walk directory trees and match patterns without ever building a regex engine. Regex engines handle arbitrary patterns including backreferences and lookaheads that require more computation. For simple wildcard file filtering, glob is always the right tool โ it's simpler to write and faster to execute.
Does VS Code use glob or regex for file search?
VS Code uses both depending on the context: the "Find" panel (Ctrl+F) uses regex when you enable the regex button; the "Files to Include/Exclude" fields in Find in Files use glob patterns. The files.exclude and search.exclude settings in settings.json also use glob. When writing VS Code tasks or launch configs with pattern fields, check the documentation โ some fields accept glob, some accept minimatch (an extended glob with additional features), and some accept regex.