Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Glob (programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Syntax== The most common wildcards are {{code|*}}, {{code|?}}, and {{code|[β¦]}}. {| class="wikitable" |- ! Wildcard ! Description ! Example ! Matches ! Does not match |- | rowspan=2 | {{code|*}} | rowspan=2 | matches any number of any characters including none | {{code|Law*}} | {{code|Law}}, {{code|Laws}}, or {{code|Lawyer}} | {{code|GrokLaw}}, {{code|La}}, or {{code|aw}} |- | {{code|*Law*}} | {{code|Law}}, {{code|GrokLaw}}, or {{code|Lawyer}}. | {{code|La}}, or {{code|aw}} |- | {{code|?}} | matches any single character | {{code|?at}} | {{code|Cat}}, {{code|cat}}, {{code|Bat}} or {{code|bat}} | {{code|at}} |- | {{code|[abc]}} | matches one character given in the bracket | {{code|[CB]at}} | {{code|Cat}} or {{code|Bat}} | {{code|cat}}, {{code|bat}} or {{code|CBat}} |- | {{code|[a-z]}} | matches one character from the (locale-dependent) range given in the bracket | {{code|Letter[0-9]}} | {{code|Letter0}}, {{code|Letter1}}, {{code|Letter2}} up to {{code|Letter9}} | {{code|Letters}}, {{code|Letter}} or {{code|Letter10}} |} Normally, the path separator character ({{code|/}} on Linux/Unix, MacOS, etc. or {{code|\}} on Windows) will never be matched. Some shells, such as [[Bourne shell|Unix shell]] have functionality allowing users to circumvent this.<ref>https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching {{Webarchive|url=https://web.archive.org/web/20180315115230/http://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching |date=2018-03-15 }} Bash Reference Manual</ref> ===Unix-like{{anchor|Unix}}=== On [[Unix-like]] systems {{code|*}}, {{code|?}} is defined as above while {{code|[β¦]}} has two additional meanings:<ref name="posixglob"/><ref name="linuxglob7"/> {| class="wikitable" |- ! Wildcard ! Description ! Example ! Matches ! Does not match |- | {{code|[!abc]}} | matches one character that is not given in the bracket | {{code|[!C]at}} | {{code|Bat}}, {{code|bat}}, or {{code|cat}} | {{code|Cat}} |- | {{code|[!a-z]}} | matches one character that is not from the range given in the bracket | {{code|Letter[!3-5]}} | {{code|Letter1}}, {{code|Letter2}}, {{code|Letter6}} up to {{code|Letter9}} and {{code|Letterx}} etc. | {{code|Letter3}}, {{code|Letter4}}, {{code|Letter5}} or {{code|Letterxx}} |} The ranges are also allowed to include pre-defined character classes, equivalence classes for accented characters, and collation symbols for hard-to-type characters. They are defined to match up with the brackets in POSIX regular expressions.<ref name="posixglob"/><ref name="linuxglob7"/> Unix globbing is handled by the [[Shell (computing)|shell]] per POSIX tradition. Globbing is provided on filenames at the [[command line interface|command line]] and in [[shell script]]s.<ref name="ABSGlob"/> The POSIX-mandated <code>case</code> statement in shells provides pattern-matching using glob patterns. Some shells (such as the [[C shell]] and [[Bash_(Unix_shell)|Bash]]) support additional syntax known as [[alternation (string expansion)|alternation]] or [[brace expansion]]. Because it is not part of the glob syntax, it is not provided in <code>case</code>. It is only expanded on the command line before globbing. The Bash shell also supports the following extensions:<ref>{{cite web |title=Bash globs |url=https://mywiki.wooledge.org/glob |website=greg's bash knowledgebase |access-date=25 November 2019 |archive-date=2019-11-18 |archive-url=https://web.archive.org/web/20191118054712/http://mywiki.wooledge.org/glob |url-status=live }}</ref> * Extended globbing (extglob): allows other pattern matching operators to be used to match multiple occurrences of a pattern enclosed in parentheses, essentially providing the missing [[kleene star]] and alternation for describing regular languages. It can be enabled by setting the {{code|extglob}} shell option. This option came from ksh93.<ref name="bashpat"/> The GNU fnmatch and glob has an identical extension.<ref name=fnmatch3/> * globstar: allows <code>**</code> on its own as a name component to recursively match any number of layers of non-hidden directories.<ref name="bashpat"/> Also supported by the [[JavaScript]] libraries and [[Python (programming language)|Python]]'s glob. ===Windows and DOS=== [[File:IBM PC DOS 1.0 screenshot.png|thumb|The [[dir (command)|{{code|dir}}]] command with a glob pattern in [[IBM PC DOS]] 1.0.]] The original [[DOS]] was a clone of [[CP/M]] designed to work on Intel's [[Intel 8088|8088]] and [[Intel 8086|8086]] processors. Windows shells, following DOS, do not traditionally perform any glob expansion in arguments passed to external programs. Shells may use an expansion for their own builtin commands: * [[Windows PowerShell]] has all the common syntax defined as stated above without any additions.<ref name="pwshcmdlet"/> * [[COMMAND.COM]] and [[cmd.exe]] have most of the common syntax with some limitations: There is no {{code|[β¦]}} and for COMMAND.COM the {{code|*}} may only appear at the end of the pattern. It can not appear in the middle of a pattern, except immediately preceding the [[filename extension]] separator dot. Windows and DOS programs receive a long command-line string instead of argv-style parameters, and it is their responsibility to perform any splitting, quoting, or glob expansion. There is technically no fixed way of describing wildcards in programs since they are free to do what they wish. Two common glob expanders include:<ref name="winWildcard"/> * The Microsoft C Runtime (msvcrt) command-line expander, which only supports {{code|?}} and {{code|*}}.<ref>{{cite web |title=Wildcard Expansion |url=https://docs.microsoft.com/en-us/cpp/cpp/wildcard-expansion |website=docs.microsoft.com |date=8 February 2022 |language=en-us}}</ref> Both [[ReactOS]] (crt/misc/getargs.c) and [[Wine (software)|Wine]] (msvcrt/data.c) contain a compatible open-source implementation of {{code|__getmainargs}}, the function operating under-the-hood, in their core CRT. * The [[Cygwin]] and MSYS {{code|dcrt0.cc}} command-line expander, which uses the unix-style {{code|glob()}} routine under-the-hood, after splitting the arguments. Most other parts of Windows, including the Indexing Service, use the MS-DOS style of wildcards found in CMD. A relic of the 8.3 filename age, this syntax pays special attention to dots in the pattern and the text (filename). Internally this is done using three extra wildcard characters, {{code|<>"}}. On the Windows API end, the {{tt|glob()}} equivalent is {{tt|FindFirstFile}}, and {{tt|fnmatch()}} corresponds to its underlying {{tt|RtlIsNameInExpression}}.<ref>[https://blogs.msdn.microsoft.com/jeremykuhne/2017/06/04/wildcards-in-windows/ Wildcards in Windows] {{Webarchive|url=https://web.archive.org/web/20191224091832/https://blogs.msdn.microsoft.com/jeremykuhne/2017/06/04/wildcards-in-windows/ |date=2019-12-24 }}. MSDN Devblog.</ref> (Another fnmatch analogue is {{tt|PathMatchSpec}}.) Both open-source msvcrt expanders use {{tt|FindFirstFile}}, so 8.3 filename quirks will also apply in them. ===SQL=== The [[SQL]] {{code|LIKE}} operator has an equivalent to {{code|?}} and {{code|*}} but not {{code|[β¦]}}. {| class="wikitable" |- ! Common wildcard ! SQL wildcard ! Description |- | {{code|?}} | {{code|_}} | matches any single character |- | {{code|*}} | {{code|%}} | matches any number of any characters including none |} Standard SQL uses a glob-like syntax for simple string matching in its <code>LIKE</code> operator, although the term "glob" is not generally used in the SQL community. The percent sign ({{code|%}}) matches zero or more characters and the underscore ({{code|_}}) matches exactly one. Many implementations of SQL have extended the <code>LIKE</code> operator to allow a richer pattern-matching language, incorporating character ranges ({{code|[β¦]}}), their negation, and elements of regular expressions.<ref name="transact-sql-like"/>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)