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