]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/regex.7
Start of man-pages-5.02: updating .Announce and .lsm files
[thirdparty/man-pages.git] / man7 / regex.7
CommitLineData
fea681da
MK
1.\" From Henry Spencer's regex package (as found in the apache
2.\" distribution). The package carries the following copyright:
3.\"
4.\" Copyright 1992, 1993, 1994 Henry Spencer. All rights reserved.
1a459d04 5.\" %%%LICENSE_START(MISC)
fea681da
MK
6.\" This software is not subject to any license of the American Telephone
7.\" and Telegraph Company or of the Regents of the University of California.
c13182ef 8.\"
fea681da
MK
9.\" Permission is granted to anyone to use this software for any purpose
10.\" on any computer system, and to alter it and redistribute it, subject
11.\" to the following restrictions:
c13182ef 12.\"
fea681da
MK
13.\" 1. The author is not responsible for the consequences of use of this
14.\" software, no matter how awful, even if they arise from flaws in it.
c13182ef 15.\"
fea681da
MK
16.\" 2. The origin of this software must not be misrepresented, either by
17.\" explicit claim or by omission. Since few users ever read sources,
18.\" credits must appear in the documentation.
c13182ef 19.\"
fea681da
MK
20.\" 3. Altered versions must be plainly marked as such, and must not be
21.\" misrepresented as being the original software. Since few users
22.\" ever read sources, credits must appear in the documentation.
c13182ef 23.\"
fea681da 24.\" 4. This notice may not be removed or altered.
8ff7380d 25.\" %%%LICENSE_END
c13182ef 26.\"
fea681da
MK
27.\" In order to comply with `credits must appear in the documentation'
28.\" I added an AUTHOR paragraph below - aeb.
29.\"
30.\" In the default nroff environment there is no dagger \(dg.
bf6c69c9
MK
31.\"
32.\" 2005-05-11 Removed discussion of `[[:<:]]' and `[[:>:]]', which
33.\" appear not to be in the glibc implementation of regcomp
34.\"
fea681da
MK
35.ie t .ds dg \(dg
36.el .ds dg (!)
e0c674cd 37.TH REGEX 7 2009-01-12 "" "Linux Programmer's Manual"
fea681da 38.SH NAME
4dec66f9 39regex \- POSIX.2 regular expressions
fea681da 40.SH DESCRIPTION
324633ae 41Regular expressions ("RE"s),
4dec66f9 42as defined in POSIX.2, come in two forms:
fea681da
MK
43modern REs (roughly those of
44.IR egrep ;
324633ae 45POSIX.2 calls these "extended" REs)
fea681da
MK
46and obsolete REs (roughly those of
47.BR ed (1);
324633ae 48POSIX.2 "basic" REs).
fea681da
MK
49Obsolete REs mostly exist for backward compatibility in some old programs;
50they will be discussed at the end.
fa203d85 51POSIX.2 leaves some aspects of RE syntax and semantics open;
333a424b 52"\*(dg" marks decisions on these aspects that
fa203d85 53may not be fully portable to other POSIX.2 implementations.
fea681da 54.PP
aa796481 55A (modern) RE is one\*(dg or more nonempty\*(dg \fIbranches\fR,
333a424b 56separated by \(aq|\(aq.
fea681da
MK
57It matches anything that matches one of the branches.
58.PP
59A branch is one\*(dg or more \fIpieces\fR, concatenated.
f78ed33a
MK
60It matches a match for the first, followed by a match for the second,
61and so on.
fea681da
MK
62.PP
63A piece is an \fIatom\fR possibly followed
333a424b 64by a single\*(dg \(aq*\(aq, \(aq+\(aq, \(aq?\(aq, or \fIbound\fR.
c45660d7
MK
65An atom followed by \(aq*\(aq
66matches a sequence of 0 or more matches of the atom.
67An atom followed by \(aq+\(aq
68matches a sequence of 1 or more matches of the atom.
69An atom followed by \(aq?\(aq
70matches a sequence of 0 or 1 matches of the atom.
fea681da 71.PP
333a424b
MK
72A \fIbound\fR is \(aq{\(aq followed by an unsigned decimal integer,
73possibly followed by \(aq,\(aq
fea681da 74possibly followed by another unsigned decimal integer,
333a424b 75always followed by \(aq}\(aq.
097585ed
MK
76The integers must lie between 0 and
77.B RE_DUP_MAX
78(255\*(dg) inclusive,
fea681da
MK
79and if there are two of them, the first may not exceed the second.
80An atom followed by a bound containing one integer \fIi\fR
81and no comma matches
82a sequence of exactly \fIi\fR matches of the atom.
83An atom followed by a bound
84containing one integer \fIi\fR and a comma matches
85a sequence of \fIi\fR or more matches of the atom.
86An atom followed by a bound
87containing two integers \fIi\fR and \fIj\fR matches
88a sequence of \fIi\fR through \fIj\fR (inclusive) matches of the atom.
89.PP
c45660d7
MK
90An atom is a regular expression enclosed in "\fI()\fP"
91(matching a match for the regular expression),
333a424b
MK
92an empty set of "\fI()\fP" (matching the null string)\*(dg,
93a \fIbracket expression\fR (see below), \(aq.\(aq
94(matching any single character), \(aq^\(aq (matching the null string at the
95beginning of a line), \(aq$\(aq (matching the null string at the
31a6818e
MK
96end of a line), a \(aq\e\(aq followed by one of the characters
97"\fI^.[$()|*+?{\e\fP"
fea681da 98(matching that character taken as an ordinary character),
31a6818e 99a \(aq\e\(aq followed by any other character\*(dg
fea681da 100(matching that character taken as an ordinary character,
31a6818e 101as if the \(aq\e\(aq had not been present\*(dg),
fea681da 102or a single character with no other significance (matching that character).
333a424b 103A \(aq{\(aq followed by a character other than a digit is an ordinary
fea681da 104character, not the beginning of a bound\*(dg.
31a6818e 105It is illegal to end an RE with \(aq\e\(aq.
fea681da 106.PP
333a424b 107A \fIbracket expression\fR is a list of characters enclosed in "\fI[]\fP".
fea681da 108It normally matches any single character from the list (but see below).
333a424b 109If the list begins with \(aq^\(aq,
fea681da
MK
110it matches any single character
111(but see below) \fInot\fR from the rest of the list.
333a424b 112If two characters in the list are separated by \(aq\-\(aq, this is shorthand
fea681da
MK
113for the full \fIrange\fR of characters between those two (inclusive) in the
114collating sequence,
333a424b 115for example, "\fI[0\-9]\fP" in ASCII matches any decimal digit.
fea681da 116It is illegal\*(dg for two ranges to share an
e2a71fb3 117endpoint, for example, "\fIa\-c\-e\fP".
fea681da
MK
118Ranges are very collating-sequence-dependent,
119and portable programs should avoid relying on them.
120.PP
333a424b
MK
121To include a literal \(aq]\(aq in the list, make it the first character
122(following a possible \(aq^\(aq).
123To include a literal \(aq\-\(aq, make it the first or last character,
fea681da 124or the second endpoint of a range.
333a424b 125To use a literal \(aq\-\(aq as the first endpoint of a range,
c45660d7
MK
126enclose it in "\fI[.\fP" and "\fI.]\fP"
127to make it a collating element (see below).
333a424b 128With the exception of these and some combinations using \(aq[\(aq (see next
31a6818e 129paragraphs), all other special characters, including \(aq\e\(aq, lose their
fea681da
MK
130special significance within a bracket expression.
131.PP
132Within a bracket expression, a collating element (a character,
ae03dc66 133a multicharacter sequence that collates as if it were a single character,
fea681da 134or a collating-sequence name for either)
333a424b 135enclosed in "\fI[.\fP" and "\fI.]\fP" stands for the
fea681da
MK
136sequence of characters of that collating element.
137The sequence is a single element of the bracket expression's list.
ae03dc66 138A bracket expression containing a multicharacter collating element
fea681da 139can thus match more than one character,
333a424b
MK
140for example, if the collating sequence includes a "ch" collating element,
141then the RE "\fI[[.ch.]]*c\fP" matches the first five characters
142of "chchcc".
fea681da 143.PP
333a424b
MK
144Within a bracket expression, a collating element enclosed in "\fI[=\fP" and
145"\fI=]\fP" is an equivalence class, standing for the sequences of characters
fea681da
MK
146of all collating elements equivalent to that one, including itself.
147(If there are no other equivalent collating elements,
c45660d7
MK
148the treatment is as if the enclosing delimiters
149were "\fI[.\fP" and "\fI.]\fP".)
fea681da 150For example, if o and \o'o^' are the members of an equivalence class,
c45660d7
MK
151then "\fI[[=o=]]\fP", "\fI[[=\o'o^'=]]\fP",
152and "\fI[o\o'o^']\fP" are all synonymous.
fea681da
MK
153An equivalence class may not\*(dg be an endpoint
154of a range.
155.PP
156Within a bracket expression, the name of a \fIcharacter class\fR enclosed
c45660d7
MK
157in "\fI[:\fP" and "\fI:]\fP" stands for the list
158of all characters belonging to that
fea681da
MK
159class.
160Standard character class names are:
161.PP
162.RS
34f2dcd0
ER
163.TS
164l l l.
fea681da
MK
165alnum digit punct
166alpha graph space
167blank lower upper
168cntrl print xdigit
34f2dcd0 169.TE
fea681da
MK
170.RE
171.PP
172These stand for the character classes defined in
173.BR wctype (3).
174A locale may provide others.
175A character class may not be used as an endpoint of a range.
bf6c69c9
MK
176.\" As per http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=295666
177.\" The following does not seem to apply in the glibc implementation
178.\" .PP
179.\" There are two special cases\*(dg of bracket expressions:
c45660d7
MK
180.\" the bracket expressions "\fI[[:<:]]\fP" and "\fI[[:>:]]\fP" match
181.\" the null string at the beginning and end of a word respectively.
bf6c69c9
MK
182.\" A word is defined as a sequence of
183.\" word characters
184.\" which is neither preceded nor followed by
185.\" word characters.
186.\" A word character is an
187.\" .I alnum
188.\" character (as defined by
189.\" .BR wctype (3))
190.\" or an underscore.
191.\" This is an extension,
4dec66f9 192.\" compatible with but not specified by POSIX.2,
bf6c69c9
MK
193.\" and should be used with
194.\" caution in software intended to be portable to other systems.
fea681da
MK
195.PP
196In the event that an RE could match more than one substring of a given
197string,
198the RE matches the one starting earliest in the string.
199If the RE could match more than one substring starting at that point,
200it matches the longest.
201Subexpressions also match the longest possible substrings, subject to
202the constraint that the whole match be as long as possible,
203with subexpressions starting earlier in the RE taking priority over
204ones starting later.
205Note that higher-level subexpressions thus take priority over
206their lower-level component subexpressions.
207.PP
208Match lengths are measured in characters, not collating elements.
209A null string is considered longer than no match at all.
210For example,
333a424b 211"\fIbb*\fP" matches the three middle characters of "abbbc",
c45660d7
MK
212"\fI(wee|week)(knights|nights)\fP"
213matches all ten characters of "weeknights",
333a424b 214when "\fI(.*).*\fP" is matched against "abc" the parenthesized subexpression
fea681da 215matches all three characters, and
c45660d7
MK
216when "\fI(a*)*\fP" is matched against "bc"
217both the whole RE and the parenthesized
fea681da
MK
218subexpression match the null string.
219.PP
220If case-independent matching is specified,
221the effect is much as if all case distinctions had vanished from the
222alphabet.
223When an alphabetic that exists in multiple cases appears as an
224ordinary character outside a bracket expression, it is effectively
225transformed into a bracket expression containing both cases,
333a424b 226for example, \(aqx\(aq becomes "\fI[xX]\fP".
fea681da 227When it appears inside a bracket expression, all case counterparts
333a424b
MK
228of it are added to the bracket expression, so that, for example, "\fI[x]\fP"
229becomes "\fI[xX]\fP" and "\fI[^x]\fP" becomes "\fI[^xX]\fP".
fea681da
MK
230.PP
231No particular limit is imposed on the length of REs\*(dg.
232Programs intended to be portable should not employ REs longer
233than 256 bytes,
234as an implementation can refuse to accept such REs and remain
235POSIX-compliant.
236.PP
324633ae 237Obsolete ("basic") regular expressions differ in several respects.
c45660d7
MK
238\(aq|\(aq, \(aq+\(aq, and \(aq?\(aq are
239ordinary characters and there is no equivalent
fea681da 240for their functionality.
31a6818e 241The delimiters for bounds are "\fI\e{\fP" and "\fI\e}\fP",
333a424b 242with \(aq{\(aq and \(aq}\(aq by themselves ordinary characters.
31a6818e 243The parentheses for nested subexpressions are "\fI\e(\fP" and "\fI\e)\fP",
333a424b
MK
244with \(aq(\(aq and \(aq)\(aq by themselves ordinary characters.
245\(aq^\(aq is an ordinary character except at the beginning of the
fea681da 246RE or\*(dg the beginning of a parenthesized subexpression,
333a424b 247\(aq$\(aq is an ordinary character except at the end of the
fea681da 248RE or\*(dg the end of a parenthesized subexpression,
333a424b 249and \(aq*\(aq is an ordinary character if it appears at the beginning of the
fea681da 250RE or the beginning of a parenthesized subexpression
333a424b 251(after a possible leading \(aq^\(aq).
4f020e78 252.PP
fea681da 253Finally, there is one new type of atom, a \fIback reference\fR:
31a6818e 254\(aq\e\(aq followed by a nonzero decimal digit \fId\fR
fea681da
MK
255matches the same sequence of characters
256matched by the \fId\fRth parenthesized subexpression
257(numbering subexpressions by the positions of their opening parentheses,
258left to right),
31a6818e 259so that, for example, "\fI\e([bc]\e)\e1\fP" matches "bb" or "cc" but not "bc".
fea681da
MK
260.SH BUGS
261Having two kinds of REs is a botch.
262.PP
333a424b
MK
263The current POSIX.2 spec says that \(aq)\(aq is an ordinary character in
264the absence of an unmatched \(aq(\(aq;
fea681da
MK
265this was an unintentional result of a wording error,
266and change is likely.
267Avoid relying on it.
268.PP
269Back references are a dreadful botch,
270posing major problems for efficient implementations.
271They are also somewhat vaguely defined
272(does
31a6818e 273"\fIa\e(\e(b\e)*\e2\e)*d\fP" match "abbbd"?).
fea681da
MK
274Avoid using them.
275.PP
fa203d85 276POSIX.2's specification of case-independent matching is vague.
324633ae 277The "one case implies all cases" definition given above
fea681da 278is current consensus among implementors as to the right interpretation.
4f020e78
MK
279.\" As per http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=295666
280.\" The following does not seem to apply in the glibc implementation
281.\" .PP
282.\" The syntax for word boundaries is incredibly ugly.
e0c674cd
MK
283.SH AUTHOR
284.\" Sigh... The page license means we must have the author's name
285.\" in the formatted output.
286This page was taken from Henry Spencer's regex package.
47297adb 287.SH SEE ALSO
845d36d6 288.BR grep (1),
e37e3282
MK
289.BR regex (3)
290.PP
291POSIX.2, section 2.8 (Regular Expression Notation).