Import nvi2 version 2.1.3 to vendor branch
[dragonfly.git] / contrib / nvi2 / regex / re_format.7
1 .\"     $NetBSD: re_format.7,v 1.1.1.2 2008/05/18 14:31:37 aymeric Exp $
2 .\"
3 .\" Copyright (c) 1992, 1993, 1994 Henry Spencer.
4 .\" Copyright (c) 1992, 1993, 1994
5 .\"     The Regents of the University of California.  All rights reserved.
6 .\"
7 .\" This code is derived from software contributed to Berkeley by
8 .\" Henry Spencer of the University of Toronto.
9 .\"
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
12 .\" are met:
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\"    notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\"    notice, this list of conditions and the following disclaimer in the
17 .\"    documentation and/or other materials provided with the distribution.
18 .\" 3. Neither the name of the University nor the names of its contributors
19 .\"    may be used to endorse or promote products derived from this software
20 .\"    without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\"
34 .\"     @(#)re_format.7 8.2 (Berkeley) 3/16/94
35 .\"
36 .TH RE_FORMAT 7 "March 16, 1994"
37 .SH NAME
38 re_format \- POSIX 1003.2 regular expressions
39 .SH DESCRIPTION
40 Regular expressions (``RE''s),
41 as defined in POSIX 1003.2, come in two forms:
42 modern REs (roughly those of
43 .IR egrep ;
44 1003.2 calls these ``extended'' REs)
45 and obsolete REs (roughly those of
46 .IR ed ;
47 1003.2 ``basic'' REs).
48 Obsolete REs mostly exist for backward compatibility in some old programs;
49 they will be discussed at the end.
50 1003.2 leaves some aspects of RE syntax and semantics open;
51 `\(dg' marks decisions on these aspects that
52 may not be fully portable to other 1003.2 implementations.
53 .PP
54 A (modern) RE is one\(dg or more non-empty\(dg \fIbranches\fR,
55 separated by `|'.
56 It matches anything that matches one of the branches.
57 .PP
58 A branch is one\(dg or more \fIpieces\fR, concatenated.
59 It matches a match for the first, followed by a match for the second, etc.
60 .PP
61 A piece is an \fIatom\fR possibly followed
62 by a single\(dg `*', `+', `?', or \fIbound\fR.
63 An atom followed by `*' matches a sequence of 0 or more matches of the atom.
64 An atom followed by `+' matches a sequence of 1 or more matches of the atom.
65 An atom followed by `?' matches a sequence of 0 or 1 matches of the atom.
66 .PP
67 A \fIbound\fR is `{' followed by an unsigned decimal integer,
68 possibly followed by `,'
69 possibly followed by another unsigned decimal integer,
70 always followed by `}'.
71 The integers must lie between 0 and RE_DUP_MAX (255\(dg) inclusive,
72 and if there are two of them, the first may not exceed the second.
73 An atom followed by a bound containing one integer \fIi\fR
74 and no comma matches
75 a sequence of exactly \fIi\fR matches of the atom.
76 An atom followed by a bound
77 containing one integer \fIi\fR and a comma matches
78 a sequence of \fIi\fR or more matches of the atom.
79 An atom followed by a bound
80 containing two integers \fIi\fR and \fIj\fR matches
81 a sequence of \fIi\fR through \fIj\fR (inclusive) matches of the atom.
82 .PP
83 An atom is a regular expression enclosed in `()' (matching a match for the
84 regular expression),
85 an empty set of `()' (matching the null string)\(dg,
86 a \fIbracket expression\fR (see below), `.'
87 (matching any single character), `^' (matching the null string at the
88 beginning of a line), `$' (matching the null string at the
89 end of a line), a `\e' followed by one of the characters
90 `^.[$()|*+?{\e'
91 (matching that character taken as an ordinary character),
92 a `\e' followed by any other character\(dg
93 (matching that character taken as an ordinary character,
94 as if the `\e' had not been present\(dg),
95 or a single character with no other significance (matching that character).
96 A `{' followed by a character other than a digit is an ordinary
97 character, not the beginning of a bound\(dg.
98 It is illegal to end an RE with `\e'.
99 .PP
100 A \fIbracket expression\fR is a list of characters enclosed in `[]'.
101 It normally matches any single character from the list (but see below).
102 If the list begins with `^',
103 it matches any single character
104 (but see below) \fInot\fR from the rest of the list.
105 If two characters in the list are separated by `\-', this is shorthand
106 for the full \fIrange\fR of characters between those two (inclusive) in the
107 collating sequence,
108 e.g. `[0-9]' in ASCII matches any decimal digit.
109 It is illegal\(dg for two ranges to share an
110 endpoint, e.g. `a-c-e'.
111 Ranges are very collating-sequence-dependent,
112 and portable programs should avoid relying on them.
113 .PP
114 To include a literal `]' in the list, make it the first character
115 (following a possible `^').
116 To include a literal `\-', make it the first or last character,
117 or the second endpoint of a range.
118 To use a literal `\-' as the first endpoint of a range,
119 enclose it in `[.' and `.]' to make it a collating element (see below).
120 With the exception of these and some combinations using `[' (see next
121 paragraphs), all other special characters, including `\e', lose their
122 special significance within a bracket expression.
123 .PP
124 Within a bracket expression, a collating element (a character,
125 a multi-character sequence that collates as if it were a single character,
126 or a collating-sequence name for either)
127 enclosed in `[.' and `.]' stands for the
128 sequence of characters of that collating element.
129 The sequence is a single element of the bracket expression's list.
130 A bracket expression containing a multi-character collating element 
131 can thus match more than one character,
132 e.g. if the collating sequence includes a `ch' collating element,
133 then the RE `[[.ch.]]*c' matches the first five characters
134 of `chchcc'.
135 .PP
136 Within a bracket expression, a collating element enclosed in `[=' and
137 `=]' is an equivalence class, standing for the sequences of characters
138 of all collating elements equivalent to that one, including itself.
139 (If there are no other equivalent collating elements,
140 the treatment is as if the enclosing delimiters were `[.' and `.]'.)
141 For example, if o and \o'o^' are the members of an equivalence class,
142 then `[[=o=]]', `[[=\o'o^'=]]', and `[o\o'o^']' are all synonymous.
143 An equivalence class may not\(dg be an endpoint
144 of a range.
145 .PP
146 Within a bracket expression, the name of a \fIcharacter class\fR enclosed
147 in `[:' and `:]' stands for the list of all characters belonging to that
148 class.
149 Standard character class names are:
150 .PP
151 .RS
152 .nf
153 .ta 3c 6c 9c
154 alnum   digit   punct
155 alpha   graph   space
156 blank   lower   upper
157 cntrl   print   xdigit
158 .fi
159 .RE
160 .PP
161 These stand for the character classes defined in
162 .IR ctype (3).
163 A locale may provide others.
164 A character class may not be used as an endpoint of a range.
165 .PP
166 There are two special cases\(dg of bracket expressions:
167 the bracket expressions `[[:<:]]' and `[[:>:]]' match the null string at
168 the beginning and end of a word respectively.
169 A word is defined as a sequence of
170 word characters
171 which is neither preceded nor followed by
172 word characters.
173 A word character is an
174 .I alnum
175 character (as defined by
176 .IR ctype (3))
177 or an underscore.
178 This is an extension,
179 compatible with but not specified by POSIX 1003.2,
180 and should be used with
181 caution in software intended to be portable to other systems.
182 .PP
183 In the event that an RE could match more than one substring of a given
184 string,
185 the RE matches the one starting earliest in the string.
186 If the RE could match more than one substring starting at that point,
187 it matches the longest.
188 Subexpressions also match the longest possible substrings, subject to
189 the constraint that the whole match be as long as possible,
190 with subexpressions starting earlier in the RE taking priority over
191 ones starting later.
192 Note that higher-level subexpressions thus take priority over
193 their lower-level component subexpressions.
194 .PP
195 Match lengths are measured in characters, not collating elements.
196 A null string is considered longer than no match at all.
197 For example,
198 `bb*' matches the three middle characters of `abbbc',
199 `(wee|week)(knights|nights)' matches all ten characters of `weeknights',
200 when `(.*).*' is matched against `abc' the parenthesized subexpression
201 matches all three characters, and
202 when `(a*)*' is matched against `bc' both the whole RE and the parenthesized
203 subexpression match the null string.
204 .PP
205 If case-independent matching is specified,
206 the effect is much as if all case distinctions had vanished from the
207 alphabet.
208 When an alphabetic that exists in multiple cases appears as an
209 ordinary character outside a bracket expression, it is effectively
210 transformed into a bracket expression containing both cases,
211 e.g. `x' becomes `[xX]'.
212 When it appears inside a bracket expression, all case counterparts
213 of it are added to the bracket expression, so that (e.g.) `[x]'
214 becomes `[xX]' and `[^x]' becomes `[^xX]'.
215 .PP
216 No particular limit is imposed on the length of REs\(dg.
217 Programs intended to be portable should not employ REs longer
218 than 256 bytes,
219 as an implementation can refuse to accept such REs and remain
220 POSIX-compliant.
221 .PP
222 Obsolete (``basic'') regular expressions differ in several respects.
223 `|', `+', and `?' are ordinary characters and there is no equivalent
224 for their functionality.
225 The delimiters for bounds are `\e{' and `\e}',
226 with `{' and `}' by themselves ordinary characters.
227 The parentheses for nested subexpressions are `\e(' and `\e)',
228 with `(' and `)' by themselves ordinary characters.
229 `^' is an ordinary character except at the beginning of the
230 RE or\(dg the beginning of a parenthesized subexpression,
231 `$' is an ordinary character except at the end of the
232 RE or\(dg the end of a parenthesized subexpression,
233 and `*' is an ordinary character if it appears at the beginning of the
234 RE or the beginning of a parenthesized subexpression
235 (after a possible leading `^').
236 Finally, there is one new type of atom, a \fIback reference\fR:
237 `\e' followed by a non-zero decimal digit \fId\fR
238 matches the same sequence of characters
239 matched by the \fId\fRth parenthesized subexpression
240 (numbering subexpressions by the positions of their opening parentheses,
241 left to right),
242 so that (e.g.) `\e([bc]\e)\e1' matches `bb' or `cc' but not `bc'.
243 .SH SEE ALSO
244 regex(3)
245 .PP
246 POSIX 1003.2, section 2.8 (Regular Expression Notation).
247 .SH BUGS
248 Having two kinds of REs is a botch.
249 .PP
250 The current 1003.2 spec says that `)' is an ordinary character in
251 the absence of an unmatched `(';
252 this was an unintentional result of a wording error,
253 and change is likely.
254 Avoid relying on it.
255 .PP
256 Back references are a dreadful botch,
257 posing major problems for efficient implementations.
258 They are also somewhat vaguely defined
259 (does
260 `a\e(\e(b\e)*\e2\e)*d' match `abbbd'?).
261 Avoid using them.
262 .PP
263 1003.2's specification of case-independent matching is vague.
264 The ``one case implies all cases'' definition given above
265 is current consensus among implementors as to the right interpretation.
266 .PP
267 The syntax for word boundaries is incredibly ugly.