1 .\" Copyright (c) 1992, 1993, 1994 Henry Spencer.
2 .\" Copyright (c) 1992, 1993, 1994
3 .\" The Regents of the University of California. All rights reserved.
5 .\" This code is derived from software contributed to Berkeley by
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)re_format.7 8.3 (Berkeley) 3/20/94
44 .Nd POSIX 1003.2 regular expressions
51 modern REs (roughly those of
56 and obsolete REs (roughly those of
61 Obsolete REs mostly exist for backward compatibility in some old programs;
62 they will be discussed at the end.
64 leaves some aspects of RE syntax and semantics open;
65 `\(dd' marks decisions on these aspects that
66 may not be fully portable to other
70 A (modern) RE is one\(dd or more non-empty\(dd
74 It matches anything that matches one of the branches.
76 A branch is one\(dd or more
79 It matches a match for the first, followed by a match for the second, etc.
92 matches a sequence of 0 or more matches of the atom.
95 matches a sequence of 1 or more matches of the atom.
98 matches a sequence of 0 or 1 matches of the atom.
104 followed by an unsigned decimal integer,
107 possibly followed by another unsigned decimal integer,
110 The integers must lie between 0 and
113 and if there are two of them, the first may not exceed the second.
114 An atom followed by a bound containing one integer
117 a sequence of exactly
120 An atom followed by a bound
121 containing one integer
126 or more matches of the atom.
127 An atom followed by a bound
128 containing two integers
137 (inclusive) matches of the atom.
139 An atom is a regular expression enclosed in
141 (matching a match for the
145 (matching the null string)\(dd,
147 .Em bracket expression
150 (matching any single character),
152 (matching the null string at the beginning of a line),
154 (matching the null string at the end of a line), a
156 followed by one of the characters
158 (matching that character taken as an ordinary character),
161 followed by any other character\(dd
162 (matching that character taken as an ordinary character,
165 had not been present\(dd),
166 or a single character with no other significance (matching that character).
169 followed by a character other than a digit is an ordinary
170 character, not the beginning of a bound\(dd.
171 It is illegal to end an RE with
175 .Em bracket expression
176 is a list of characters enclosed in
178 It normally matches any single character from the list (but see below).
179 If the list begins with
181 it matches any single character
184 from the rest of the list.
185 If two characters in the list are separated by
190 of characters between those two (inclusive) in the
193 in ASCII matches any decimal digit.
194 It is illegal\(dd for two ranges to share an
197 Ranges are very collating-sequence-dependent,
198 and portable programs should avoid relying on them.
202 in the list, make it the first character
203 (following a possible
207 make it the first or last character,
208 or the second endpoint of a range.
211 as the first endpoint of a range,
216 to make it a collating element (see below).
217 With the exception of these and some combinations using
219 (see next paragraphs), all other special characters, including
221 lose their special significance within a bracket expression.
223 Within a bracket expression, a collating element (a character,
224 a multi-character sequence that collates as if it were a single character,
225 or a collating-sequence name for either)
231 sequence of characters of that collating element.
232 The sequence is a single element of the bracket expression's list.
233 A bracket expression containing a multi-character collating element
234 can thus match more than one character,
235 e.g.\& if the collating sequence includes a
240 matches the first five characters
244 Within a bracket expression, a collating element enclosed in
248 is an equivalence class, standing for the sequences of characters
249 of all collating elements equivalent to that one, including itself.
250 (If there are no other equivalent collating elements,
251 the treatment is as if the enclosing delimiters were
259 are the members of an equivalence class,
266 An equivalence class may not\(dd be an endpoint
269 Within a bracket expression, the name of a
275 stands for the list of all characters belonging to that
277 Standard character class names are:
278 .Bl -column "alnum" "digit" "xdigit" -offset indent
279 .It Em "alnum digit punct"
280 .It Em "alpha graph space"
281 .It Em "blank lower upper"
282 .It Em "cntrl print xdigit"
285 These stand for the character classes defined in
287 A locale may provide others.
288 A character class may not be used as an endpoint of a range.
290 A bracketed expression like
292 can be used to match a single character that belongs to a character
294 The reverse, matching any character that does not belong to a specific
295 class, the negation operator of bracket expressions may be used:
298 There are two special cases\(dd of bracket expressions:
299 the bracket expressions
303 match the null string at the beginning and end of a word respectively.
304 A word is defined as a sequence of word characters
305 which is neither preceded nor followed by
307 A word character is an
309 character (as defined by
312 This is an extension,
313 compatible with but not specified by
315 and should be used with
316 caution in software intended to be portable to other systems.
318 In the event that an RE could match more than one substring of a given
320 the RE matches the one starting earliest in the string.
321 If the RE could match more than one substring starting at that point,
322 it matches the longest.
323 Subexpressions also match the longest possible substrings, subject to
324 the constraint that the whole match be as long as possible,
325 with subexpressions starting earlier in the RE taking priority over
327 Note that higher-level subexpressions thus take priority over
328 their lower-level component subexpressions.
330 Match lengths are measured in characters, not collating elements.
331 A null string is considered longer than no match at all.
334 matches the three middle characters of
336 .Ql (wee|week)(knights|nights)
337 matches all ten characters of
343 the parenthesized subexpression
344 matches all three characters, and
349 both the whole RE and the parenthesized
350 subexpression match the null string.
352 If case-independent matching is specified,
353 the effect is much as if all case distinctions had vanished from the
355 When an alphabetic that exists in multiple cases appears as an
356 ordinary character outside a bracket expression, it is effectively
357 transformed into a bracket expression containing both cases,
361 When it appears inside a bracket expression, all case counterparts
362 of it are added to the bracket expression, so that (e.g.)
371 No particular limit is imposed on the length of REs\(dd.
372 Programs intended to be portable should not employ REs longer
374 as an implementation can refuse to accept such REs and remain
379 regular expressions differ in several respects.
381 is an ordinary character and there is no equivalent
382 for its functionality.
386 are ordinary characters, and their functionality
387 can be expressed using bounds
394 in modern REs is equivalent to
396 The delimiters for bounds are
404 by themselves ordinary characters.
405 The parentheses for nested subexpressions are
413 by themselves ordinary characters.
415 is an ordinary character except at the beginning of the
416 RE or\(dd the beginning of a parenthesized subexpression,
418 is an ordinary character except at the end of the
419 RE or\(dd the end of a parenthesized subexpression,
422 is an ordinary character if it appears at the beginning of the
423 RE or the beginning of a parenthesized subexpression
424 (after a possible leading
426 Finally, there is one new type of atom, a
429 followed by a non-zero decimal digit
431 matches the same sequence of characters
434 parenthesized subexpression
435 (numbering subexpressions by the positions of their opening parentheses,
448 .%T Regular Expression Notation
454 Having two kinds of REs is a botch.
460 is an ordinary character in
461 the absence of an unmatched
463 this was an unintentional result of a wording error,
464 and change is likely.
467 Back references are a dreadful botch,
468 posing major problems for efficient implementations.
469 They are also somewhat vaguely defined
471 .Ql a\e(\e(b\e)*\e2\e)*d
477 specification of case-independent matching is vague.
479 .Dq one case implies all cases
480 definition given above
481 is current consensus among implementors as to the right interpretation.
483 The syntax for word boundaries is incredibly ugly.