Glob vs Regex: How the Two Pattern Languages Differ and When to Use Each
Glob patterns match file names; regular expressions match text. Learn how their syntax differs, how to translate between them, and where patterns break.
Glob patterns and regular expressions both describe sets of strings, and they share symbols such as the asterisk, question mark and square brackets. They are separate languages with different goals, though. Globs are a small, fixed notation built mainly for matching file and path names. Regular expressions are a general-purpose language for searching, validating and rewriting any text. Reusing a symbol from one in the other is a common source of patterns that look right but match the wrong things.
What each pattern actually matches
A glob is compared against a whole name. The pattern *.txt means the entire file name must end in .txt. It does not mean the name only has to contain .txt somewhere. In path-aware globbing, which is what shells and most file-search libraries do, wildcards stop at the directory separator. A single * stays within one path segment.
- matches any run of characters, including none, within one path segment
- ? matches exactly one character
- [abc] matches one character from the set; [a-z] matches one character from a range
- [!abc] matches one character not in the set (POSIX form; some shells, such as bash, also accept [^abc])
- ** matches across directory levels, but only in tools that support it, such as bash with the globstar option, zsh and .gitignore files. It is not part of POSIX globbing.
- {a,b} looks like part of glob syntax, but in shells such as bash it is a separate step called brace expansion, which runs before globbing
A regular expression is usually applied as a search. Unless it is anchored, it can match anywhere inside the input. Its symbols are building blocks: some match characters, and others say how many times the preceding element repeats.
- . matches any single character (by default, usually any character except a newline)
- repeats the preceding element zero or more times; + means one or more; ? means zero or one
- {n,m} repeats the preceding element between n and m times
- [abc] and [^abc] are positive and negated character classes
- ^ and $ anchor the match to the start and end of the input or line
- | expresses alternation, and parentheses group elements and capture what they matched
- Many flavors also offer backreferences, lookarounds, and shorthand classes such as \d and \s
Same symbols, different meanings
| Symbol | In a glob | In a regex |
| • | Any characters within a segment | Repeat the previous element zero or more times |
| ? | Exactly one character | Make the previous element optional, or make a quantifier lazy |
| . | A literal dot | Any single character |
| [!x] | Any one character except x | A character class containing ! and x |
| [^x] | Negation in some shells, not guaranteed by POSIX | Any one character except x |
| Anchoring | Implicit: the whole name must match | Explicit: requires ^ and $ |
Hidden files are another difference. In POSIX shells, * and ? do not match a leading dot, so *.conf will not match .bashrc. You have to write the dot explicitly or turn on an option such as bash's dotglob. Regular expressions have no rule like this, so a regex that matches "any name" will also match dot-files.
# Glob: every .log file in the current directory
ls *.log
# Regex: lines in a text file that end in ".log"
grep -E '\.log$' filelist.txt
# A glob written as a regex is wrong or invalid:
# in a regex, "*.log" starts with a quantifier that has nothing to repeat
Translating a glob into a regex
Any basic glob can be rewritten as a regex. Anchor both ends, escape literal dots, and replace each wildcard with a class that excludes the path separator. The reverse does not always work. Globs have no general way to express repetition counts, alternation inside a segment (unless the tool offers braces or extended globbing), or optional groups, so many regexes have no glob equivalent.
Glob Roughly equivalent path-aware regex
*.txt ^[^/]*\.txt$
data-??.csv ^data-[^/][^/]\.csv$
report[0-9]* ^report[0-9][^/]*$
These translations are approximate. They ignore the leading-dot rule and any tool-specific features such as ** or case-insensitive matching. Some languages include a converter; for example, Python's fnmatch module provides a translate function. Note that fnmatch-style matching does not treat the slash specially, unlike path-aware globbing. If exact behavior matters, test the converted pattern against real names.
Choosing between them
- Selecting files by name or extension (shell commands, build configs, ignore files): use a glob. It is shorter, and the tool expects it.
- Searching inside file contents, or checking a string's format: use a regex.
- You need counts, alternatives, optional parts or captured pieces: use a regex.
- A glob is almost enough but you need "one of these words": check whether the tool supports brace expansion or extended globbing (for example, bash's extglob forms such as @(jpg|png)) before switching to a regex.
- The tool's documentation names one syntax: use that one. Many tools accept only globs or only regexes, and a few accept both through a flag.
Will the pattern work where you paste it?
Neither language is fully portable. Glob behavior differs between shells and libraries: support for **, negated classes, case-insensitivity and dot-file handling varies. Unmatched globs also behave differently. Bash leaves the pattern as literal text by default, while zsh reports an error by default. Regular expressions come in flavors, including POSIX basic and extended, PCRE-style engines, JavaScript, Python and RE2-style engines. They differ on lookarounds, backreferences, shorthand classes and escaping rules. A pattern that passes in one tester can fail or behave differently in another engine, so check the destination's documented flavor.
Shell quoting is the most common way the two languages collide. When a regex is passed unquoted on the command line, the shell may treat it as a glob and expand it against files in the current directory before the program runs. Single quotes pass the pattern through unchanged.
# Unquoted: the shell may expand a*.txt against local files first
grep a*.txt notes.md
# Quoted: grep receives the regex exactly as written
grep 'a.*\.txt' notes.md
Copying patterns between contexts can also change them silently:
• Documents and chat apps may turn straight quotes into curly quotes, which shells do not recognize as quoting characters.
• Pasted text can carry non-breaking or zero-width spaces, which a pattern matches literally.
• In many programming languages, a regex placed inside an ordinary string literal needs its backslashes doubled. JSON, YAML and similar config formats can require extra escaping too.
When a pattern fails only after being moved, retype the quotes and inspect the backslashes first.
Readability is a real concern, especially with regexes, which quickly become hard for others to review. Several flavors support a verbose or extended mode that allows whitespace and comments inside the pattern, such as Python's re.VERBOSE and the x flag in PCRE-style engines. Keeping a short list of strings that should and should not match makes a pattern easier to check. If a glob does the job, it is usually the more readable choice.
Web-based regex testers are convenient, but anything pasted into them is sent to, or at least processed by, someone else's page. Avoid pasting log lines, tokens, customer records or other sensitive text. Use a made-up sample with the same structure instead, or test locally with your language's own regex engine. A local test also has the advantage of using the same flavor your code will run.
Performance can differ as well. Backtracking regex engines can take a very long time on certain patterns with nested or overlapping quantifiers. Engines designed for linear-time matching, such as RE2, avoid this but drop features like backreferences. If a regex will process untrusted input, look up how the target engine handles worst-case patterns.
Limits of this guide
This comparison covers general, widely documented behavior of glob and regex syntax. It does not test or endorse any specific tool, and no measurements or tool-specific results were available for it. Option names, defaults and flavor details change between versions and implementations, so confirm the exact behavior in the documentation of the shell, library or application you are using.