xref: /minix/external/bsd/llvm/dist/llvm/docs/re_format.7 (revision 83133719)
1.\"	$OpenBSD: re_format.7,v 1.14 2007/05/31 19:19:30 jmc Exp $
2.\"
3.\" Copyright (c) 1997, Phillip F Knaack. All rights reserved.
4.\"
5.\" Copyright (c) 1992, 1993, 1994 Henry Spencer.
6.\" Copyright (c) 1992, 1993, 1994
7.\"	The Regents of the University of California.  All rights reserved.
8.\"
9.\" This code is derived from software contributed to Berkeley by
10.\" Henry Spencer.
11.\"
12.\" Redistribution and use in source and binary forms, with or without
13.\" modification, are permitted provided that the following conditions
14.\" are met:
15.\" 1. Redistributions of source code must retain the above copyright
16.\"    notice, this list of conditions and the following disclaimer.
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in the
19.\"    documentation and/or other materials provided with the distribution.
20.\" 3. 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.
23.\"
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
34.\" SUCH DAMAGE.
35.\"
36.\"	@(#)re_format.7	8.3 (Berkeley) 3/20/94
37.\"
38.Dd $Mdocdate: May 31 2007 $
39.Dt RE_FORMAT 7
40.Os
41.Sh NAME
42.Nm re_format
43.Nd POSIX regular expressions
44.Sh DESCRIPTION
45Regular expressions (REs),
46as defined in
47.St -p1003.1-2004 ,
48come in two forms:
49basic regular expressions
50(BREs)
51and extended regular expressions
52(EREs).
53Both forms of regular expressions are supported
54by the interfaces described in
55.Xr regex 3 .
56Applications dealing with regular expressions
57may use one or the other form
58(or indeed both).
59For example,
60.Xr ed 1
61uses BREs,
62whilst
63.Xr egrep 1
64talks EREs.
65Consult the manual page for the specific application to find out which
66it uses.
67.Pp
68POSIX leaves some aspects of RE syntax and semantics open;
69.Sq **
70marks decisions on these aspects that
71may not be fully portable to other POSIX implementations.
72.Pp
73This manual page first describes regular expressions in general,
74specifically extended regular expressions,
75and then discusses differences between them and basic regular expressions.
76.Sh EXTENDED REGULAR EXPRESSIONS
77An ERE is one** or more non-empty**
78.Em branches ,
79separated by
80.Sq \*(Ba .
81It matches anything that matches one of the branches.
82.Pp
83A branch is one** or more
84.Em pieces ,
85concatenated.
86It matches a match for the first, followed by a match for the second, etc.
87.Pp
88A piece is an
89.Em atom
90possibly followed by a single**
91.Sq * ,
92.Sq + ,
93.Sq ?\& ,
94or
95.Em bound .
96An atom followed by
97.Sq *
98matches a sequence of 0 or more matches of the atom.
99An atom followed by
100.Sq +
101matches a sequence of 1 or more matches of the atom.
102An atom followed by
103.Sq ?\&
104matches a sequence of 0 or 1 matches of the atom.
105.Pp
106A bound is
107.Sq {
108followed by an unsigned decimal integer,
109possibly followed by
110.Sq ,\&
111possibly followed by another unsigned decimal integer,
112always followed by
113.Sq } .
114The integers must lie between 0 and
115.Dv RE_DUP_MAX
116(255**) inclusive,
117and if there are two of them, the first may not exceed the second.
118An atom followed by a bound containing one integer
119.Ar i
120and no comma matches
121a sequence of exactly
122.Ar i
123matches of the atom.
124An atom followed by a bound
125containing one integer
126.Ar i
127and a comma matches
128a sequence of
129.Ar i
130or more matches of the atom.
131An atom followed by a bound
132containing two integers
133.Ar i
134and
135.Ar j
136matches a sequence of
137.Ar i
138through
139.Ar j
140(inclusive) matches of the atom.
141.Pp
142An atom is a regular expression enclosed in
143.Sq ()
144(matching a part of the regular expression),
145an empty set of
146.Sq ()
147(matching the null string)**,
148a
149.Em bracket expression
150(see below),
151.Sq .\&
152(matching any single character),
153.Sq ^
154(matching the null string at the beginning of a line),
155.Sq $
156(matching the null string at the end of a line),
157a
158.Sq \e
159followed by one of the characters
160.Sq ^.[$()|*+?{\e
161(matching that character taken as an ordinary character),
162a
163.Sq \e
164followed by any other character**
165(matching that character taken as an ordinary character,
166as if the
167.Sq \e
168had not been present**),
169or a single character with no other significance (matching that character).
170A
171.Sq {
172followed by a character other than a digit is an ordinary character,
173not the beginning of a bound**.
174It is illegal to end an RE with
175.Sq \e .
176.Pp
177A bracket expression is a list of characters enclosed in
178.Sq [] .
179It normally matches any single character from the list (but see below).
180If the list begins with
181.Sq ^ ,
182it matches any single character
183.Em not
184from the rest of the list
185(but see below).
186If two characters in the list are separated by
187.Sq - ,
188this is shorthand for the full
189.Em range
190of characters between those two (inclusive) in the
191collating sequence, e.g.\&
192.Sq [0-9]
193in ASCII matches any decimal digit.
194It is illegal** for two ranges to share an endpoint, e.g.\&
195.Sq a-c-e .
196Ranges are very collating-sequence-dependent,
197and portable programs should avoid relying on them.
198.Pp
199To include a literal
200.Sq ]\&
201in the list, make it the first character
202(following a possible
203.Sq ^ ) .
204To include a literal
205.Sq - ,
206make it the first or last character,
207or the second endpoint of a range.
208To use a literal
209.Sq -
210as the first endpoint of a range,
211enclose it in
212.Sq [.
213and
214.Sq .]
215to make it a collating element (see below).
216With the exception of these and some combinations using
217.Sq [
218(see next paragraphs),
219all other special characters, including
220.Sq \e ,
221lose their special significance within a bracket expression.
222.Pp
223Within a bracket expression, a collating element
224(a character,
225a multi-character sequence that collates as if it were a single character,
226or a collating-sequence name for either)
227enclosed in
228.Sq [.
229and
230.Sq .]
231stands for the sequence of characters of that collating element.
232The sequence is a single element of the bracket expression's list.
233A bracket expression containing a multi-character collating element
234can thus match more than one character,
235e.g. if the collating sequence includes a
236.Sq ch
237collating element,
238then the RE
239.Sq [[.ch.]]*c
240matches the first five characters of
241.Sq chchcc .
242.Pp
243Within a bracket expression, a collating element enclosed in
244.Sq [=
245and
246.Sq =]
247is an equivalence class, standing for the sequences of characters
248of all collating elements equivalent to that one, including itself.
249(If there are no other equivalent collating elements,
250the treatment is as if the enclosing delimiters were
251.Sq [.
252and
253.Sq .] . )
254For example, if
255.Sq x
256and
257.Sq y
258are the members of an equivalence class,
259then
260.Sq [[=x=]] ,
261.Sq [[=y=]] ,
262and
263.Sq [xy]
264are all synonymous.
265An equivalence class may not** be an endpoint of a range.
266.Pp
267Within a bracket expression, the name of a
268.Em character class
269enclosed
270in
271.Sq [:
272and
273.Sq :]
274stands for the list of all characters belonging to that class.
275Standard character class names are:
276.Bd -literal -offset indent
277alnum	digit	punct
278alpha	graph	space
279blank	lower	upper
280cntrl	print	xdigit
281.Ed
282.Pp
283These stand for the character classes defined in
284.Xr ctype 3 .
285A locale may provide others.
286A character class may not be used as an endpoint of a range.
287.Pp
288There are two special cases** of bracket expressions:
289the bracket expressions
290.Sq [[:<:]]
291and
292.Sq [[:>:]]
293match the null string at the beginning and end of a word, respectively.
294A word is defined as a sequence of
295characters starting and ending with a word character
296which is neither preceded nor followed by
297word characters.
298A word character is an
299.Em alnum
300character (as defined by
301.Xr ctype 3 )
302or an underscore.
303This is an extension,
304compatible with but not specified by POSIX,
305and should be used with
306caution in software intended to be portable to other systems.
307.Pp
308In the event that an RE could match more than one substring of a given
309string,
310the RE matches the one starting earliest in the string.
311If the RE could match more than one substring starting at that point,
312it matches the longest.
313Subexpressions also match the longest possible substrings, subject to
314the constraint that the whole match be as long as possible,
315with subexpressions starting earlier in the RE taking priority over
316ones starting later.
317Note that higher-level subexpressions thus take priority over
318their lower-level component subexpressions.
319.Pp
320Match lengths are measured in characters, not collating elements.
321A null string is considered longer than no match at all.
322For example,
323.Sq bb*
324matches the three middle characters of
325.Sq abbbc ;
326.Sq (wee|week)(knights|nights)
327matches all ten characters of
328.Sq weeknights ;
329when
330.Sq (.*).*
331is matched against
332.Sq abc ,
333the parenthesized subexpression matches all three characters;
334and when
335.Sq (a*)*
336is matched against
337.Sq bc ,
338both the whole RE and the parenthesized subexpression match the null string.
339.Pp
340If case-independent matching is specified,
341the effect is much as if all case distinctions had vanished from the
342alphabet.
343When an alphabetic that exists in multiple cases appears as an
344ordinary character outside a bracket expression, it is effectively
345transformed into a bracket expression containing both cases,
346e.g.\&
347.Sq x
348becomes
349.Sq [xX] .
350When it appears inside a bracket expression,
351all case counterparts of it are added to the bracket expression,
352so that, for example,
353.Sq [x]
354becomes
355.Sq [xX]
356and
357.Sq [^x]
358becomes
359.Sq [^xX] .
360.Pp
361No particular limit is imposed on the length of REs**.
362Programs intended to be portable should not employ REs longer
363than 256 bytes,
364as an implementation can refuse to accept such REs and remain
365POSIX-compliant.
366.Pp
367The following is a list of extended regular expressions:
368.Bl -tag -width Ds
369.It Ar c
370Any character
371.Ar c
372not listed below matches itself.
373.It \e Ns Ar c
374Any backslash-escaped character
375.Ar c
376matches itself.
377.It \&.
378Matches any single character that is not a newline
379.Pq Sq \en .
380.It Bq Ar char-class
381Matches any single character in
382.Ar char-class .
383To include a
384.Ql \&]
385in
386.Ar char-class ,
387it must be the first character.
388A range of characters may be specified by separating the end characters
389of the range with a
390.Ql - ;
391e.g.\&
392.Ar a-z
393specifies the lower case characters.
394The following literal expressions can also be used in
395.Ar char-class
396to specify sets of characters:
397.Bd -unfilled -offset indent
398[:alnum:] [:cntrl:] [:lower:] [:space:]
399[:alpha:] [:digit:] [:print:] [:upper:]
400[:blank:] [:graph:] [:punct:] [:xdigit:]
401.Ed
402.Pp
403If
404.Ql -
405appears as the first or last character of
406.Ar char-class ,
407then it matches itself.
408All other characters in
409.Ar char-class
410match themselves.
411.Pp
412Patterns in
413.Ar char-class
414of the form
415.Eo [.
416.Ar col-elm
417.Ec .]\&
418or
419.Eo [=
420.Ar col-elm
421.Ec =]\& ,
422where
423.Ar col-elm
424is a collating element, are interpreted according to
425.Xr setlocale 3
426.Pq not currently supported .
427.It Bq ^ Ns Ar char-class
428Matches any single character, other than newline, not in
429.Ar char-class .
430.Ar char-class
431is defined as above.
432.It ^
433If
434.Sq ^
435is the first character of a regular expression, then it
436anchors the regular expression to the beginning of a line.
437Otherwise, it matches itself.
438.It $
439If
440.Sq $
441is the last character of a regular expression,
442it anchors the regular expression to the end of a line.
443Otherwise, it matches itself.
444.It [[:<:]]
445Anchors the single character regular expression or subexpression
446immediately following it to the beginning of a word.
447.It [[:>:]]
448Anchors the single character regular expression or subexpression
449immediately following it to the end of a word.
450.It Pq Ar re
451Defines a subexpression
452.Ar re .
453Any set of characters enclosed in parentheses
454matches whatever the set of characters without parentheses matches
455(that is a long-winded way of saying the constructs
456.Sq (re)
457and
458.Sq re
459match identically).
460.It *
461Matches the single character regular expression or subexpression
462immediately preceding it zero or more times.
463If
464.Sq *
465is the first character of a regular expression or subexpression,
466then it matches itself.
467The
468.Sq *
469operator sometimes yields unexpected results.
470For example, the regular expression
471.Ar b*
472matches the beginning of the string
473.Qq abbb
474(as opposed to the substring
475.Qq bbb ) ,
476since a null match is the only leftmost match.
477.It +
478Matches the singular character regular expression
479or subexpression immediately preceding it
480one or more times.
481.It ?
482Matches the singular character regular expression
483or subexpression immediately preceding it
4840 or 1 times.
485.Sm off
486.It Xo
487.Pf { Ar n , m No }\ \&
488.Pf { Ar n , No }\ \&
489.Pf { Ar n No }
490.Xc
491.Sm on
492Matches the single character regular expression or subexpression
493immediately preceding it at least
494.Ar n
495and at most
496.Ar m
497times.
498If
499.Ar m
500is omitted, then it matches at least
501.Ar n
502times.
503If the comma is also omitted, then it matches exactly
504.Ar n
505times.
506.It \*(Ba
507Used to separate patterns.
508For example,
509the pattern
510.Sq cat\*(Badog
511matches either
512.Sq cat
513or
514.Sq dog .
515.El
516.Sh BASIC REGULAR EXPRESSIONS
517Basic regular expressions differ in several respects:
518.Bl -bullet -offset 3n
519.It
520.Sq \*(Ba ,
521.Sq + ,
522and
523.Sq ?\&
524are ordinary characters and there is no equivalent
525for their functionality.
526.It
527The delimiters for bounds are
528.Sq \e{
529and
530.Sq \e} ,
531with
532.Sq {
533and
534.Sq }
535by themselves ordinary characters.
536.It
537The parentheses for nested subexpressions are
538.Sq \e(
539and
540.Sq \e) ,
541with
542.Sq (
543and
544.Sq )\&
545by themselves ordinary characters.
546.It
547.Sq ^
548is an ordinary character except at the beginning of the
549RE or** the beginning of a parenthesized subexpression.
550.It
551.Sq $
552is an ordinary character except at the end of the
553RE or** the end of a parenthesized subexpression.
554.It
555.Sq *
556is an ordinary character if it appears at the beginning of the
557RE or the beginning of a parenthesized subexpression
558(after a possible leading
559.Sq ^ ) .
560.It
561Finally, there is one new type of atom, a
562.Em back-reference :
563.Sq \e
564followed by a non-zero decimal digit
565.Ar d
566matches the same sequence of characters matched by the
567.Ar d Ns th
568parenthesized subexpression
569(numbering subexpressions by the positions of their opening parentheses,
570left to right),
571so that, for example,
572.Sq \e([bc]\e)\e1
573matches
574.Sq bb\&
575or
576.Sq cc
577but not
578.Sq bc .
579.El
580.Pp
581The following is a list of basic regular expressions:
582.Bl -tag -width Ds
583.It Ar c
584Any character
585.Ar c
586not listed below matches itself.
587.It \e Ns Ar c
588Any backslash-escaped character
589.Ar c ,
590except for
591.Sq { ,
592.Sq } ,
593.Sq \&( ,
594and
595.Sq \&) ,
596matches itself.
597.It \&.
598Matches any single character that is not a newline
599.Pq Sq \en .
600.It Bq Ar char-class
601Matches any single character in
602.Ar char-class .
603To include a
604.Ql \&]
605in
606.Ar char-class ,
607it must be the first character.
608A range of characters may be specified by separating the end characters
609of the range with a
610.Ql - ;
611e.g.\&
612.Ar a-z
613specifies the lower case characters.
614The following literal expressions can also be used in
615.Ar char-class
616to specify sets of characters:
617.Bd -unfilled -offset indent
618[:alnum:] [:cntrl:] [:lower:] [:space:]
619[:alpha:] [:digit:] [:print:] [:upper:]
620[:blank:] [:graph:] [:punct:] [:xdigit:]
621.Ed
622.Pp
623If
624.Ql -
625appears as the first or last character of
626.Ar char-class ,
627then it matches itself.
628All other characters in
629.Ar char-class
630match themselves.
631.Pp
632Patterns in
633.Ar char-class
634of the form
635.Eo [.
636.Ar col-elm
637.Ec .]\&
638or
639.Eo [=
640.Ar col-elm
641.Ec =]\& ,
642where
643.Ar col-elm
644is a collating element, are interpreted according to
645.Xr setlocale 3
646.Pq not currently supported .
647.It Bq ^ Ns Ar char-class
648Matches any single character, other than newline, not in
649.Ar char-class .
650.Ar char-class
651is defined as above.
652.It ^
653If
654.Sq ^
655is the first character of a regular expression, then it
656anchors the regular expression to the beginning of a line.
657Otherwise, it matches itself.
658.It $
659If
660.Sq $
661is the last character of a regular expression,
662it anchors the regular expression to the end of a line.
663Otherwise, it matches itself.
664.It [[:<:]]
665Anchors the single character regular expression or subexpression
666immediately following it to the beginning of a word.
667.It [[:>:]]
668Anchors the single character regular expression or subexpression
669immediately following it to the end of a word.
670.It \e( Ns Ar re Ns \e)
671Defines a subexpression
672.Ar re .
673Subexpressions may be nested.
674A subsequent backreference of the form
675.Pf \e Ns Ar n ,
676where
677.Ar n
678is a number in the range [1,9], expands to the text matched by the
679.Ar n Ns th
680subexpression.
681For example, the regular expression
682.Ar \e(.*\e)\e1
683matches any string consisting of identical adjacent substrings.
684Subexpressions are ordered relative to their left delimiter.
685.It *
686Matches the single character regular expression or subexpression
687immediately preceding it zero or more times.
688If
689.Sq *
690is the first character of a regular expression or subexpression,
691then it matches itself.
692The
693.Sq *
694operator sometimes yields unexpected results.
695For example, the regular expression
696.Ar b*
697matches the beginning of the string
698.Qq abbb
699(as opposed to the substring
700.Qq bbb ) ,
701since a null match is the only leftmost match.
702.Sm off
703.It Xo
704.Pf \e{ Ar n , m No \e}\ \&
705.Pf \e{ Ar n , No \e}\ \&
706.Pf \e{ Ar n No \e}
707.Xc
708.Sm on
709Matches the single character regular expression or subexpression
710immediately preceding it at least
711.Ar n
712and at most
713.Ar m
714times.
715If
716.Ar m
717is omitted, then it matches at least
718.Ar n
719times.
720If the comma is also omitted, then it matches exactly
721.Ar n
722times.
723.El
724.Sh SEE ALSO
725.Xr ctype 3 ,
726.Xr regex 3
727.Sh STANDARDS
728.St -p1003.1-2004 :
729Base Definitions, Chapter 9 (Regular Expressions).
730.Sh BUGS
731Having two kinds of REs is a botch.
732.Pp
733The current POSIX spec says that
734.Sq )\&
735is an ordinary character in the absence of an unmatched
736.Sq ( ;
737this was an unintentional result of a wording error,
738and change is likely.
739Avoid relying on it.
740.Pp
741Back-references are a dreadful botch,
742posing major problems for efficient implementations.
743They are also somewhat vaguely defined
744(does
745.Sq a\e(\e(b\e)*\e2\e)*d
746match
747.Sq abbbd ? ) .
748Avoid using them.
749.Pp
750POSIX's specification of case-independent matching is vague.
751The
752.Dq one case implies all cases
753definition given above
754is the current consensus among implementors as to the right interpretation.
755.Pp
756The syntax for word boundaries is incredibly ugly.
757