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