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