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. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)re_format.7 8.3 (Berkeley) 3/20/94
33 .\" $FreeBSD: head/lib/libc/regex/re_format.7 282007 2015-04-26 10:55:39Z bapt $
40 .Nd POSIX 1003.2 regular expressions
47 modern REs (roughly those of
52 and obsolete REs (roughly those of
57 Obsolete REs mostly exist for backward compatibility in some old programs;
58 they will be discussed at the end.
60 leaves some aspects of RE syntax and semantics open;
61 `\(dd' marks decisions on these aspects that
62 may not be fully portable to other
66 A (modern) RE is one\(dd or more non-empty\(dd
70 It matches anything that matches one of the branches.
72 A branch is one\(dd or more
75 It matches a match for the first, followed by a match for the second, etc.
88 matches a sequence of 0 or more matches of the atom.
91 matches a sequence of 1 or more matches of the atom.
94 matches a sequence of 0 or 1 matches of the atom.
100 followed by an unsigned decimal integer,
103 possibly followed by another unsigned decimal integer,
106 The integers must lie between 0 and
109 and if there are two of them, the first may not exceed the second.
110 An atom followed by a bound containing one integer
113 a sequence of exactly
116 An atom followed by a bound
117 containing one integer
122 or more matches of the atom.
123 An atom followed by a bound
124 containing two integers
133 (inclusive) matches of the atom.
135 An atom is a regular expression enclosed in
137 (matching a match for the
141 (matching the null string)\(dd,
143 .Em bracket expression
146 (matching any single character),
148 (matching the null string at the beginning of a line),
150 (matching the null string at the end of a line), a
152 followed by one of the characters
154 (matching that character taken as an ordinary character),
157 followed by any other character\(dd
158 (matching that character taken as an ordinary character,
161 had not been present\(dd),
162 or a single character with no other significance (matching that character).
165 followed by a character other than a digit is an ordinary
166 character, not the beginning of a bound\(dd.
167 It is illegal to end an RE with
171 .Em bracket expression
172 is a list of characters enclosed in
174 It normally matches any single character from the list (but see below).
175 If the list begins with
177 it matches any single character
180 from the rest of the list.
181 If two characters in the list are separated by
186 of characters between those two (inclusive) in the
189 in ASCII matches any decimal digit.
190 It is illegal\(dd for two ranges to share an
193 Ranges are very collating-sequence-dependent,
194 and portable programs should avoid relying on them.
198 in the list, make it the first character
199 (following a possible
203 make it the first or last character,
204 or the second endpoint of a range.
207 as the first endpoint of a range,
212 to make it a collating element (see below).
213 With the exception of these and some combinations using
215 (see next paragraphs), all other special characters, including
217 lose their special significance within a bracket expression.
219 Within a bracket expression, a collating element (a character,
220 a multi-character sequence that collates as if it were a single character,
221 or a collating-sequence name for either)
227 sequence of characters of that collating element.
228 The sequence is a single element of the bracket expression's list.
229 A bracket expression containing a multi-character collating element
230 can thus match more than one character,
231 e.g.\& if the collating sequence includes a
236 matches the first five characters
240 Within a bracket expression, a collating element enclosed in
244 is an equivalence class, standing for the sequences of characters
245 of all collating elements equivalent to that one, including itself.
246 (If there are no other equivalent collating elements,
247 the treatment is as if the enclosing delimiters were
255 are the members of an equivalence class,
262 An equivalence class may not\(dd be an endpoint
265 Within a bracket expression, the name of a
271 stands for the list of all characters belonging to that
273 Standard character class names are:
274 .Bl -column "alnum" "digit" "xdigit" -offset indent
275 .It Em "alnum digit punct"
276 .It Em "alpha graph space"
277 .It Em "blank lower upper"
278 .It Em "cntrl print xdigit"
281 These stand for the character classes defined in
283 A locale may provide others.
284 A character class may not be used as an endpoint of a range.
286 A bracketed expression like
288 can be used to match a single character that belongs to a character
290 The reverse, matching any character that does not belong to a specific
291 class, the negation operator of bracket expressions may be used:
294 There are two special cases\(dd of bracket expressions:
295 the bracket expressions
299 match the null string at the beginning and end of a word respectively.
300 A word is defined as a sequence of word characters
301 which is neither preceded nor followed by
303 A word character is an
305 character (as defined by
308 This is an extension,
309 compatible with but not specified by
311 and should be used with
312 caution in software intended to be portable to other systems.
313 The additional word delimiters
317 are provided to ease compatibility with traditional
319 systems but are not portable and should be avoided.
321 In the event that an RE could match more than one substring of a given
323 the RE matches the one starting earliest in the string.
324 If the RE could match more than one substring starting at that point,
325 it matches the longest.
326 Subexpressions also match the longest possible substrings, subject to
327 the constraint that the whole match be as long as possible,
328 with subexpressions starting earlier in the RE taking priority over
330 Note that higher-level subexpressions thus take priority over
331 their lower-level component subexpressions.
333 Match lengths are measured in characters, not collating elements.
334 A null string is considered longer than no match at all.
337 matches the three middle characters of
339 .Ql (wee|week)(knights|nights)
340 matches all ten characters of
346 the parenthesized subexpression
347 matches all three characters, and
352 both the whole RE and the parenthesized
353 subexpression match the null string.
355 If case-independent matching is specified,
356 the effect is much as if all case distinctions had vanished from the
358 When an alphabetic that exists in multiple cases appears as an
359 ordinary character outside a bracket expression, it is effectively
360 transformed into a bracket expression containing both cases,
364 When it appears inside a bracket expression, all case counterparts
365 of it are added to the bracket expression, so that (e.g.)
374 No particular limit is imposed on the length of REs\(dd.
375 Programs intended to be portable should not employ REs longer
377 as an implementation can refuse to accept such REs and remain
382 regular expressions differ in several respects.
384 is an ordinary character and there is no equivalent
385 for its functionality.
389 are ordinary characters, and their functionality
390 can be expressed using bounds
397 in modern REs is equivalent to
399 The delimiters for bounds are
407 by themselves ordinary characters.
408 The parentheses for nested subexpressions are
416 by themselves ordinary characters.
418 is an ordinary character except at the beginning of the
419 RE or\(dd the beginning of a parenthesized subexpression,
421 is an ordinary character except at the end of the
422 RE or\(dd the end of a parenthesized subexpression,
425 is an ordinary character if it appears at the beginning of the
426 RE or the beginning of a parenthesized subexpression
427 (after a possible leading
429 Finally, there is one new type of atom, a
432 followed by a non-zero decimal digit
434 matches the same sequence of characters
437 parenthesized subexpression
438 (numbering subexpressions by the positions of their opening parentheses,
451 .%T Regular Expression Notation
457 Having two kinds of REs is a botch.
463 is an ordinary character in
464 the absence of an unmatched
466 this was an unintentional result of a wording error,
467 and change is likely.
470 Back references are a dreadful botch,
471 posing major problems for efficient implementations.
472 They are also somewhat vaguely defined
474 .Ql a\e(\e(b\e)*\e2\e)*d
480 specification of case-independent matching is vague.
482 .Dq one case implies all cases
483 definition given above
484 is current consensus among implementors as to the right interpretation.
486 The syntax for word boundaries is incredibly ugly.