.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "STRING-MATCH" "1" "Nov 13, 2025" "4.2" "fish-shell" .SH NAME string-match \- match substrings .SH SYNOPSIS .nf \fBstring\fP \fBmatch\fP [\fB\-a\fP | \fB\-\-all\fP] [\fB\-e\fP | \fB\-\-entire\fP] [\fB\-i\fP | \fB\-\-ignore\-case\fP] [\fB\-g\fP | \fB\-\-groups\-only\fP] [\fB\-r\fP | \fB\-\-regex\fP] [\fB\-n\fP | \fB\-\-index\fP] [\fB\-q\fP | \fB\-\-quiet\fP] [\fB\-v\fP | \fB\-\-invert\fP] [(\fB\-m\fP | \fB\-\-max\-matches\fP) \fIMAX\fP] \fIPATTERN\fP [\fISTRING\fP \&...] .fi .sp .SH DESCRIPTION .sp \fBstring match\fP tests each \fISTRING\fP against \fIPATTERN\fP and prints matching substrings. Only the first match for each \fISTRING\fP is reported unless \fB\-a\fP or \fB\-\-all\fP is given, in which case all matches are reported. .sp If you specify the \fB\-e\fP or \fB\-\-entire\fP then each matching string is printed including any prefix or suffix not matched by the pattern (equivalent to \fBgrep\fP without the \fB\-o\fP flag). You can, obviously, achieve the same result by prepending and appending \fB*\fP or \fB\&.*\fP depending on whether or not you have specified the \fB\-\-regex\fP flag. The \fB\-\-entire\fP flag is a way to avoid having to complicate the pattern in that fashion and make the intent of the \fBstring match\fP clearer. Without \fB\-\-entire\fP and \fB\-\-regex\fP, a \fIPATTERN\fP will need to match the entire \fISTRING\fP before it will be reported. .sp Matching can be made case\-insensitive with \fB\-\-ignore\-case\fP or \fB\-i\fP\&. .sp If \fB\-\-groups\-only\fP or \fB\-g\fP is given, only the capturing groups will be reported \- meaning the full match will be skipped. This is incompatible with \fB\-\-entire\fP and \fB\-\-invert\fP, and requires \fB\-\-regex\fP\&. It is useful as a simple cutting tool instead of \fBstring replace\fP, so you can choose \(dqthis part\(dq of a string. .sp If \fB\-\-index\fP or \fB\-n\fP is given, each match is reported as a 1\-based start position and a length. By default, PATTERN is interpreted as a glob pattern matched against each entire \fISTRING\fP argument. A glob pattern is only considered a valid match if it matches the entire \fISTRING\fP\&. .sp If \fB\-\-regex\fP or \fB\-r\fP is given, \fIPATTERN\fP is interpreted as a Perl\-compatible regular expression, which does not have to match the entire \fISTRING\fP\&. For a regular expression containing capturing groups, multiple items will be reported for each match, one for the entire match and one for each capturing group. With this, only the matching part of the \fISTRING\fP will be reported, unless \fB\-\-entire\fP is given. .sp When matching via regular expressions, \fBstring match\fP automatically sets variables for all named capturing groups (\fB(?expression)\fP). It will create a variable with the name of the group, in the default scope, for each named capturing group, and set it to the value of the capturing group in the first matched argument. If a named capture group matched an empty string, the variable will be set to the empty string (like \fBset var \(dq\(dq\fP). If it did not match, the variable will be set to nothing (like \fBset var\fP). When \fB\-\-regex\fP is used with \fB\-\-all\fP, this behavior changes. Each named variable will contain a list of matches, with the first match contained in the first element, the second match in the second, and so on. If the group was empty or did not match, the corresponding element will be an empty string. .sp If \fB\-\-invert\fP or \fB\-v\fP is used the selected lines will be only those which do not match the given glob pattern or regular expression. .sp If \fB\-\-max\-matches MAX\fP or \fB\-m MAX\fP is used, \fBstring\fP will stop checking for matches after MAX lines of input have matched. This can be used as an \(dqearly exit\(dq optimization when processing long inputs but expecting a limited and fixed number of outputs that might be found considerably before the input stream has been exhausted. If combined with \fB\-\-invert\fP or \fB\-v\fP, considers only inverted matches. .sp Exit status: 0 if at least one match was found, or 1 otherwise. .SH EXAMPLES .SS Match Glob Examples .INDENT 0.0 .INDENT 3.5 .sp .EX >_ string match \(aqa\(aq a a >_ string match \(aqa*b\(aq axxb axxb >_ string match \-i \(aqa*B\(aq Axxb Axxb >_ string match \-\- \(aq\-*\(aq \-h foo \-\-version bar # To match things that look like options, we need a \(ga\-\-\(ga # to tell string its options end there. \-h \-\-version >_ echo \(aqok?\(aq | string match \(aq*?\(aq ok? # Note that only the second STRING will match here. >_ string match \(aqfoo\(aq \(aqfoo1\(aq \(aqfoo\(aq \(aqfoo2\(aq foo >_ string match \-e \(aqfoo\(aq \(aqfoo1\(aq \(aqfoo\(aq \(aqfoo2\(aq foo1 foo foo2 >_ string match \(aqfoo*\(aq \(aqfoo1\(aq \(aqfoo\(aq \(aqfoo2\(aq foo1 foo2 .EE .UNINDENT .UNINDENT .SS Match Regex Examples .INDENT 0.0 .INDENT 3.5 .sp .EX >_ string match \-r \(aqcat|dog|fish\(aq \(aqnice dog\(aq dog >_ string match \-r \-v \(dqc.*[12]\(dq {cat,dog}(seq 1 4) dog1 dog2 cat3 dog3 cat4 dog4 >_ string match \-r \-\- \(aq\-.*\(aq \-h foo \-\-version bar # To match things that look like options, we need a \(ga\-\-\(ga # to tell string its options end there. \-h \-\-version >_ string match \-r \(aq(\ed\ed?):(\ed\ed):(\ed\ed)\(aq 2:34:56 2:34:56 2 34 56 >_ string match \-r \(aq^(\ew{2,4})\e1$\(aq papa mud murmur papa pa murmur mur >_ string match \-r \-a \-n at ratatat 2 2 4 2 6 2 >_ string match \-r \-i \(aq0x[0\-9a\-f]{1,8}\(aq \(aqint magic = 0xBadC0de;\(aq 0xBadC0de >_ echo $version 3.1.2\-1575\-ga2ff32d90 >_ string match \-rq \(aq(?\ed+).(?\ed+).(?\ed+)\(aq \-\- $version >_ echo \(dqYou are using fish $major!\(dq You are using fish 3! >_ string match \-raq \(aq *(?[^.!?]+)(?[.!?])?\(aq \(dqhello, friend. goodbye\(dq >_ printf \(dq%s\en\(dq \-\- $sentence hello, friend goodbye >_ printf \(dq%s\en\(dq \-\- $punctuation \&. >_ string match \-rq \(aq(?hello)\(aq \(aqhi\(aq >_ count $word 0 .EE .UNINDENT .UNINDENT .SH COPYRIGHT fish-shell developers .\" Generated by docutils manpage writer. .