Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / man / man5 / regex.5
blobae69ddd34f0e765114669aaa53362d017a50e405
1 '\" te
2 .\" Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
3 .\" Portions Copyright (c) 1999, Sun Microsystems, Inc.  All Rights Reserved
4 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
7 .\"  This notice shall appear on any product containing this material.
8 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
9 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
10 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
11 .TH REGEX 5 "Apr 21, 2005"
12 .SH NAME
13 regex \- internationalized basic and extended regular expression matching
14 .SH DESCRIPTION
15 .sp
16 .LP
17 Regular Expressions (REs) provide a mechanism to select specific strings from a
18 set of character strings. The Internationalized Regular Expressions described
19 below differ from the Simple Regular Expressions described on the
20 \fBregexp\fR(5) manual page in the following ways:
21 .RS +4
22 .TP
23 .ie t \(bu
24 .el o
25 both Basic and Extended Regular Expressions are supported
26 .RE
27 .RS +4
28 .TP
29 .ie t \(bu
30 .el o
31 the Internationalization features\(emcharacter class, equivalence class, and
32 multi-character collation\(emare supported.
33 .RE
34 .sp
35 .LP
36 The Basic Regular Expression (BRE) notation and construction rules described in
37 the \fBBASIC\fR \fBREGULAR\fR \fBEXPRESSIONS\fR section apply to most utilities
38 supporting regular expressions. Some utilities, instead, support the Extended
39 Regular Expressions (ERE) described in the \fBEXTENDED\fR \fBREGULAR\fR
40 \fBEXPRESSIONS\fR section; any exceptions for both cases are noted in the
41 descriptions of the specific utilities using regular expressions. Both BREs and
42 EREs are supported by the Regular Expression Matching interfaces
43 \fBregcomp\fR(3C) and \fBregexec\fR(3C).
44 .SH BASIC REGULAR EXPRESSIONS
45 .SS "BREs Matching a Single Character"
46 .sp
47 .LP
48 A BRE ordinary character, a special character preceded by a backslash, or a
49 period matches a single character. A bracket expression matches a single
50 character or a single collating element. See \fBRE Bracket Expression\fR,
51 below.
52 .SS "BRE Ordinary Characters"
53 .sp
54 .LP
55 An ordinary character is a BRE that matches itself: any character in the
56 supported character set, except for the BRE special characters listed in \fBBRE
57 Special Characters\fR, below.
58 .sp
59 .LP
60 The interpretation of an ordinary character preceded by a backslash (\e) is
61 undefined, except for:
62 .RS +4
63 .TP
65 the characters ), (, {, and }
66 .RE
67 .RS +4
68 .TP
70 the digits 1 to 9 inclusive (see \fBBREs Matching Multiple Characters\fR,
71 below)
72 .RE
73 .RS +4
74 .TP
76 a character inside a bracket expression.
77 .RE
78 .SS "BRE Special Characters"
79 .sp
80 .LP
81 A BRE \fIspecial\fR \fIcharacter\fR has special properties in certain contexts.
82 Outside those contexts, or when preceded by a backslash, such a character will
83 be a BRE that matches the special character itself. The BRE special characters
84 and the contexts in which they have their special meaning are:
85 .sp
86 .ne 2
87 .na
88 \fB\&. [ \e\fR
89 .ad
90 .RS 12n
91 The period, left-bracket, and backslash are special except when used in a
92 bracket expression (see \fBRE Bracket Expression\fR, below). An expression
93 containing a [ that is not preceded by a backslash and is not part of a bracket
94 expression produces undefined results.
95 .RE
97 .sp
98 .ne 2
99 .na
100 \fB*\fR
102 .RS 12n
103 The asterisk is special except when used:
104 .RS +4
106 .ie t \(bu
107 .el o
108 in a bracket expression
110 .RS +4
112 .ie t \(bu
113 .el o
114 as the first character of an entire BRE (after an initial ^, if any)
116 .RS +4
118 .ie t \(bu
119 .el o
120 as the first character of a subexpression (after an initial ^, if any); see
121 \fBBREs Matching Multiple Characters\fR, below.
126 .ne 2
128 \fB\fI^\fR\fR
130 .RS 12n
131 The circumflex is special when used:
132 .RS +4
134 .ie t \(bu
135 .el o
136 as an anchor (see \fBBRE Expression Anchoring\fR, below).
138 .RS +4
140 .ie t \(bu
141 .el o
142 as the first character of a bracket expression (see \fBRE Bracket
143 Expression\fR, below).
148 .ne 2
150 \fB$\fR
152 .RS 12n
153 The dollar sign is special when used as an anchor.
156 .SS "Periods in BREs"
159 A period (\fB\&.\fR), when used outside a bracket expression, is a BRE that
160 matches any character in the supported character set except NUL.
161 .SS "RE Bracket Expression"
164 A bracket expression (an expression enclosed in square brackets, [\|]) is an RE
165 that matches a single collating element contained in the non-empty set of
166 collating elements represented by the bracket expression.
169 The following rules and definitions apply to bracket expressions:
170 .RS +4
173 A \fIbracket expression\fR is either a matching list expression or a
174 non-matching list expression. It consists of one or more expressions: collating
175 elements, collating symbols, equivalence classes, character classes, or range
176 expressions (see rule 7 below). Portable applications must not use range
177 expressions, even though all implementations support them. The right-bracket
178 (]) loses its special meaning and represents itself in a bracket expression if
179 it occurs first in the list (after an initial circumflex (^), if any).
180 Otherwise, it terminates the bracket expression, unless it appears in a
181 collating symbol (such as [.].]) or is the ending right-bracket for a collating
182 symbol, equivalence class, or character class. The special characters:
184 .in +2
186         .   *   [   \e
188 .in -2
191 (period, asterisk, left-bracket and backslash, respectively) lose their special
192 meaning within a bracket expression.
194 The character sequences:
196 .in +2
198         [.   [=    [:
200 .in -2
203 (left-bracket followed by a period, equals-sign, or colon) are special inside a
204 bracket expression and are used to delimit collating symbols, equivalence class
205 expressions, and character class expressions. These symbols must be followed by
206 a valid expression and the matching terminating sequence .], =] or :], as
207 described in the following items.
209 .RS +4
212 A \fImatching list\fR expression specifies a list that matches any one of
213 the expressions represented in the list. The first character in the list must
214 not be the circumflex. For example, \fB[abc]\fR is an RE that matches any of
215 the characters \fBa\fR, \fBb\fR or \fBc\fR.
217 .RS +4
220 A \fInon-matching list\fR expression begins with a circumflex (^), and
221 specifies a list that matches any character or collating element except for the
222 expressions represented in the list after the leading circumflex. For example,
223 [^abc] is an RE that matches any character or collating element except the
224 characters \fBa,\fR \fBb\fR, or \fBc\fR. The circumflex will have this special
225 meaning only when it occurs first in the list, immediately following the
226 left-bracket.
228 .RS +4
231 A \fIcollating symbol\fR is a collating element enclosed within
232 bracket-period ([..]) delimiters. Multi-character collating elements must be
233 represented as collating symbols when it is necessary to distinguish them from
234 a list of the individual characters that make up the multi-character collating
235 element. For example, if the string \fBch\fR is a collating element in the
236 current collation sequence with the associated collating symbol <ch>, the
237 expression [[.ch.]] will be treated as an RE matching the character sequence
238 \fBch\fR, while [ch] will be treated as an RE matching \fBc\fR or \fBh\fR.
239 Collating symbols will be recognized only inside bracket expressions. This
240 implies that the RE \fB[[.ch.]]*c\fR matches the first to fifth character in
241 the string chchch. If the string is not a collating element in the current
242 collating sequence definition, or if the collating element has no characters
243 associated with it, the symbol will be treated as an invalid expression.
245 .RS +4
248 An \fIequivalence class expression\fR represents the set of collating
249 elements belonging to an equivalence class. Only primary equivalence classes
250 will be recognised. The class is expressed by enclosing any one of the
251 collating elements in the equivalence class within bracket-equal ([==])
252 delimiters. For example, if a and b belong to the same equivalence class, then
253 [[=a=]b], [[==]b] and [[==]b] will each be equivalent to [ab]. If the collating
254 element does not belong to an equivalence class, the equivalence class
255 expression will be treated as a \fIcollating symbol\fR.
257 .RS +4
260 A \fIcharacter class expression\fR represents the set of characters
261 belonging to a character class, as defined in the \fBLC_CTYPE\fR category in
262 the current locale. All character classes specified in the current locale will
263 be recognized. A character class expression is expressed as a character class
264 name enclosed within bracket-colon ([::]) delimiters.
266 The following character class expressions are supported in all locales:
271 l l l l
272 l l l l .
273 \fB[:alnum:]\fR \fB[:cntrl:]\fR \fB[:lower:]\fR \fB[:space:]\fR
274 \fB[:alpha:]\fR \fB[:digit:]\fR \fB[:print:]\fR \fB[:upper:]\fR
275 \fB[:blank:]\fR \fB[:graph:]\fR \fB[:punct:]\fR \fB[:xdigit:]\fR
278 In addition, character class expressions of the form:
280 .in +2
282         \fB[:name:]\fR
284 .in -2
287 are recognized in those locales where the \fIname\fR keyword has been given a
288 \fBcharclass\fR definition in the \fBLC_CTYPE\fR category.
290 .RS +4
293 A \fIrange expression\fR represents the set of collating elements that fall
294 between two elements in the current collation sequence, inclusively. It is
295 expressed as the starting point and the ending point separated by a hyphen
296 (\fB-\fR).
298 Range expressions must not be used in portable applications because their
299 behavior is dependent on the collating sequence. Ranges will be treated
300 according to the current collating sequence, and include such characters that
301 fall within the range based on that collating sequence, regardless of character
302 values. This, however, means that the interpretation will differ depending on
303 collating sequence. If, for instance, one collating sequence defines as a
304 variant of \fBa\fR, while another defines it as a letter following \fBz\fR,
305 then the expression [-z] is valid in the first language and invalid in the
306 second.
308 In the following, all examples assume the collation sequence specified for the
309 POSIX locale, unless another collation sequence is specifically defined.
311 The starting range point and the ending range point must be a collating element
312 or collating symbol. An equivalence class expression used as a starting or
313 ending point of a range expression produces unspecified results. An equivalence
314 class can be used portably within a bracket expression, but only outside the
315 range. For example, the unspecified expression [[=e=]\(mif] should be given as
316 [[=e=]e\(mif]. The ending range point must collate equal to or higher than the
317 starting range point; otherwise, the expression will be treated as invalid. The
318 order used is the order in which the collating elements are specified in the
319 current collation definition. One-to-many mappings (see \fBlocale\fR(5)) will
320 not be performed. For example, assuming that the character eszet is placed in
321 the collation sequence after \fBr\fR and \fBs\fR, but before \fBt\fR, and that
322 it maps to the sequence \fBss\fR for collation purposes, then the expression
323 [r\(mis] matches only \fBr\fR and \fBs\fR, but the expression [s\(mit] matches
324 \fBs\fR, \fBbeta\fR, or \fBt\fR.
326 The interpretation of range expressions where the ending range point is also
327 the starting range point of a subsequent range expression (for instance
328 [a\(mim\(mio]) is undefined.
330 The hyphen character will be treated as itself if it occurs first (after an
331 initial ^, if any) or last in the list, or as an ending range point in a range
332 expression. As examples, the expressions [\(miac] and [ac\(mi] are equivalent
333 and match any of the characters \fBa\fR, \fBc\fR, or \fB\(mi;\fR [^\(miac] and
334 [^ac\(mi] are equivalent and match any characters except \fBa\fR, \fBc\fR, or
335 \(mi; the expression [%\(mi\|\(mi] matches any of the characters between % and
336 \(mi inclusive; the expression [\(mi\|\(mi@] matches any of the characters
337 between \(mi and @ inclusive; and the expression [a\(mi\|\(mi@] is invalid,
338 because the letter \fBa\fR follows the symbol \(mi in the POSIX locale. To use
339 a hyphen as the starting range point, it must either come first in the bracket
340 expression or be specified as a collating symbol, for example:
341 [][.\(mi.]\(mi0], which matches either a right bracket or any character or
342 collating element that collates between hyphen and 0, inclusive.
344 If a bracket expression must specify both \(mi and ], the ] must be placed
345 first (after the ^, if any) and the \(mi last within the bracket expression.
349 Note: Latin-1 characters such as \(ga or ^ are not printable in some locales,
350 for example, the \fBja\fR locale.
351 .SS "BREs Matching Multiple Characters"
354 The following rules can be used to construct BREs matching multiple characters
355 from BREs matching a single character:
356 .RS +4
359 The concatenation of BREs matches the concatenation of the strings matched
360 by each component of the BRE.
362 .RS +4
365 A \fIsubexpression\fR can be defined within a BRE by enclosing it between
366 the character pairs \e( and \e) . Such a subexpression matches whatever it
367 would have matched without the \e( and \e), except that anchoring within
368 subexpressions is optional behavior; see \fBBRE Expression Anchoring\fR, below.
369 Subexpressions can be arbitrarily nested.
371 .RS +4
374 The \fIback-reference\fR expression \e\fIn\fR matches the same (possibly
375 empty) string of characters as was matched by a subexpression enclosed between
376 \e( and \e) preceding the \e\fIn\fR. The character \fIn\fR must be a digit from
377 1 to 9 inclusive, \fIn\fRth subexpression (the one that begins with the
378 \fIn\fRth \e( and ends with the corresponding paired \e)\|). The expression is
379 invalid if less than \fIn\fR subexpressions precede the \e\fIn\fR. For example,
380 the expression \|^\e(.*\e)\e1$ matches a line consisting of two adjacent
381 appearances of the same string, and the expression \e(a\e)*\e1 fails to match
382 \fBa\fR. The limit of nine back-references to subexpressions in the RE is based
383 on the use of a single digit identifier. This does not imply that only nine
384 subexpressions are allowed in REs. The following is a valid BRE with ten
385 subexpressions:
387 .in +2
389 \fB\e(\e(\e(ab\e)*c\e)*d\e)\e(ef\e)*\e(gh\e)\e{2\e}\e(ij\e)*\e(kl\e)*\e(mn\e)*\e(op\e)*\e(qr\e)*\fR
391 .in -2
395 .RS +4
398 When a BRE matching a single character, a subexpression or a back-reference
399 is followed by the special character asterisk (*), together with that asterisk
400 it matches what zero or more consecutive occurrences of the BRE would match.
401 For example, \fB[ab]*\fR \fBand\fR \fB[ab][ab]\fR are equivalent when matching
402 the string \fBab\fR.
404 .RS +4
407 When a BRE matching a single character, a subexpression, or a back-reference
408 is followed by an \fIinterval expression\fR of the format \e{\fIm\fR\e},
409 \e{\fIm\fR,\e} or \e{\fIm\fR,\fIn\fR\e}, together with that interval expression
410 it matches what repeated consecutive occurrences of the BRE would match. The
411 values of \fIm\fR and \fIn\fR will be decimal integers in the range 0 \(<=
412 \fIm\fR \(<= \fIn\fR \(<= {\fBRE_DUP_MAX\fR}, where \fIm\fR specifies the exact
413 or minimum number of occurrences and \fIn\fR specifies the maximum number of
414 occurrences. The expression \e{\fIm\fR\e} matches exactly \fIm\fR occurrences
415 of the preceding BRE, \e{\fIm\fR,\e} matches at least \fIm\fR occurrences and
416 \e{\fIm,n\fR\e} matches any number of occurrences between \fIm\fR and \fIn\fR,
417 inclusive.
419 For example, in the string \fBabababccccccd\fR, the BRE c\e{3\e} is matched by
420 characters seven to nine, the BRE \e(ab\e)\e{4,\e} is not matched at all and
421 the BRE c\e{1,3\e}d is matched by characters ten to thirteen.
425 The behavior of multiple adjacent duplication symbols \fB(\fR \fB*\fR \fB\fR
426 and intervals) produces undefined results.
427 .SS "BRE Precedence"
430 The order of precedence is as shown in the following table:
432 .in +2
436 .in -2
443 box;
444 l l
445 l l .
446 \fBBRE Precedence (from high to low)\fR
447 collation-related bracket symbols       [= =]  [: :]  [. .]
448 escaped characters      \e<\fIspecial character\fR>
449 bracket expression      [ ]
450 subexpressions/back-references  \e( \e) \e\fIn\fR
451 single-character-BRE duplication        * \e{\fIm\fR,\fIn\fR\e}
452 concatenation   
453 anchoring       ^  $
456 .SS "BRE Expression Anchoring"
459 A BRE can be limited to matching strings that begin or end a line; this is
460 called \fIanchoring\fR. The circumflex and dollar sign special characters will
461 be considered BRE anchors in the following contexts:
462 .RS +4
465 A circumflex \fB(\fR \fB^\fR \fB)\fR is an anchor when used as the first
466 character of an entire BRE. The implementation may treat circumflex as an
467 anchor when used as the first character of a subexpression. The circumflex will
468 anchor the expression to the beginning of a string; only sequences starting at
469 the first character of a string will be matched by the BRE. For example, the
470 BRE ^ab matches \fBab\fR in the string \fBabcdef,\fR but fails to match in the
471 string \fBcdefab\fR. A portable BRE must escape a leading circumflex in a
472 subexpression to match a literal circumflex.
474 .RS +4
477 A dollar sign \fB(\fR \fB$\fR \fB)\fR is an anchor when used as the last
478 character of an entire BRE. The implementation may treat a dollar sign as an
479 anchor when used as the last character of a subexpression. The dollar sign will
480 anchor the expression to the end of the string being matched; the dollar sign
481 can be said to match the end-of-string following the last character.
483 .RS +4
486 A BRE anchored by both \fB^\fR and \fB$\fR matches only an entire string.
487 For example, the BRE \fB^abcdef$\fR matches strings consisting only of
488 \fBabcdef\fR.
490 .RS +4
493 \fB^\fR and \fB$\fR are not special in subexpressions.
497 Note: The Solaris implementation does not support anchoring in BRE
498 subexpressions.
499 .SH EXTENDED REGULAR EXPRESSIONS
502 The rules specififed for BREs apply to Extended Regular Expressions (EREs) with
503 the following exceptions:
504 .RS +4
506 .ie t \(bu
507 .el o
508 The characters \fB|\fR, \fB+\fR, and \fB?\fR have special meaning, as defined
509 below.
511 .RS +4
513 .ie t \(bu
514 .el o
515 The \fB{\fR and \fB}\fR characters, when used as the duplication operator, are
516 not preceded by backslashes. The constructs \fB\e{\fR and \fB\e}\fR simply
517 match the characters \fB{\fR and \fB}\fR, respectively.
519 .RS +4
521 .ie t \(bu
522 .el o
523 The back reference operator is not supported.
525 .RS +4
527 .ie t \(bu
528 .el o
529 Anchoring (\fB^$\fR) is supported in subexpressions.
531 .SS "EREs Matching a Single Character"
534 An ERE ordinary character, a special character preceded by a backslash, or a
535 period matches a single character. A bracket expression matches a single
536 character or a single collating element. An \fIERE\fR \fImatching\fR \fIa\fR
537 \fIsingle\fR \fIcharacter\fR enclosed in parentheses matches the same as the
538 ERE without parentheses would have matched.
539 .SS "ERE Ordinary Characters"
542 An \fIordinary character\fR is an ERE that matches itself. An ordinary
543 character is any character in the supported character set, except for the ERE
544 special characters listed in \fBERE\fR \fBSpecial\fR \fBCharacters\fR below.
545 The interpretation of an ordinary character preceded by a backslash (\fB\e\fR)
546 is undefined.
547 .SS "ERE Special Characters"
550 An \fIERE\fR \fIspecial\fR \fIcharacter\fR has special properties in certain
551 contexts. Outside those contexts, or when preceded by a backslash, such a
552 character is an ERE that matches the special character itself. The extended
553 regular expression special characters and the contexts in which they have their
554 special meaning are:
556 .ne 2
558 \fB\&. [ \e (\fR
560 .RS 14n
561 The period, left-bracket, backslash, and left-parenthesis are special except
562 when used in a bracket expression (see \fBRE Bracket Expression\fR, above).
563 Outside a bracket expression, a left-parenthesis immediately followed by a
564 right-parenthesis produces undefined results.
568 .ne 2
570 \fB)\fR
572 .RS 14n
573 The right-parenthesis is special when matched with a preceding
574 left-parenthesis, both outside a bracket expression.
578 .ne 2
580 \fB* + ? {\fR
582 .RS 14n
583 The asterisk, plus-sign, question-mark, and left-brace are special except when
584 used in a bracket expression (see \fBRE Bracket Expression\fR, above). Any of
585 the following uses produce undefined results:
586 .RS +4
588 .ie t \(bu
589 .el o
590 if these characters appear first in an ERE, or immediately following a
591 vertical-line, circumflex or left-parenthesis
593 .RS +4
595 .ie t \(bu
596 .el o
597 if a left-brace is not part of a valid interval expression.
602 .ne 2
604 \fB|\fR
606 .RS 14n
607 The vertical-line is special except when used in a bracket expression (see
608 \fBRE Bracket Expression\fR, above). A vertical-line appearing first or last in
609 an ERE, or immediately following a vertical-line or a left-parenthesis, or
610 immediately preceding a right-parenthesis, produces undefined results.
614 .ne 2
616 \fB^\fR
618 .RS 14n
619 The circumflex is special when used:
620 .RS +4
622 .ie t \(bu
623 .el o
624 as an anchor (see \fBERE Expression Anchoring\fR, below).
626 .RS +4
628 .ie t \(bu
629 .el o
630 as the first character of a bracket expression (see \fBRE Bracket
631 Expression\fR, above).
636 .ne 2
638 \fB$\fR
640 .RS 14n
641 The dollar sign is special when used as an anchor.
644 .SS "Periods in EREs"
647 A period (\fB\&.\fR), when used outside a bracket expression, is an ERE that
648 matches any character in the supported character set except NUL.
649 .SS "ERE Bracket Expression"
652 The rules for ERE Bracket Expressions are the same as for Basic Regular
653 Expressions; see \fBRE Bracket Expression\fR, above).
654 .SS "EREs Matching Multiple Characters"
657 The following rules will be used to construct EREs matching multiple characters
658 from EREs matching a single character:
659 .RS +4
662 A \fIconcatenation\fR \fIof\fR \fIEREs\fR matches the concatenation of the
663 character sequences matched by each component of the ERE. A concatenation of
664 EREs enclosed in parentheses matches whatever the concatenation without the
665 parentheses matches. For example, both the ERE \fBcd\fR and the ERE \fB(cd)\fR
666 are matched by the third and fourth character of the string \fBabcdefabcdef\fR.
668 .RS +4
671 When an ERE matching a single character or an ERE enclosed in parentheses is
672 followed by the special character plus-sign (+), together with that plus-sign
673 it matches what one or more consecutive occurrences of the ERE would match. For
674 example, the ERE \fBb+(bc)\fR matches the fourth to seventh characters in the
675 string \fBacabbbcde;\fR \fB[ab]\fR + and \fB[ab][ab]*\fR are equivalent.
677 .RS +4
680 When an ERE matching a single character or an ERE enclosed in parentheses is
681 followed by the special character asterisk (\fB*\fR), together with that
682 asterisk it matches what zero or more consecutive occurrences of the ERE would
683 match. For example, the ERE \fBb*c\fR matches the first character in the string
684 \fBcabbbcde,\fR and the ERE \fBb*cd\fR matches the third to seventh characters
685 in the string \fBcabbbcdebbbbbbcdbc\fR. And, \fB[ab]*\fR and \fB[ab][ab]\fR are
686 equivalent when matching the string \fBab\fR.
688 .RS +4
691 When an ERE matching a single character or an ERE enclosed in parentheses is
692 followed by the special character question-mark (\fB?\fR), together with that
693 question-mark it matches what zero or one consecutive occurrences of the ERE
694 would match. For example, the ERE \fBb?c\fR matches the second character in the
695 string \fBacabbbcde\fR.
697 .RS +4
700 When an ERE matching a single character or an ERE enclosed in parentheses is
701 followed by an \fIinterval\fR \fIexpression\fR of the format {\fIm\fR},
702 {\fIm\fR,} or {\fIm\fR,\fIn\fR}, together with that interval expression it
703 matches what repeated consecutive occurrences of the ERE would match. The
704 values of \fIm\fR and \fIn\fR will be decimal integers in the range 0 \(<=
705 \fIm\fR \(<= \fIn\fR \(<= \fB{RE_DUP_MAX}\fR, where \fIm\fR specifies the exact
706 or minimum number of occurrences and \fIn\fR specifies the maximum number of
707 occurrences. The expression {\fIm\fR} matches exactly \fIm\fR occurrences of
708 the preceding ERE, {\fIm\fR,} matches at least \fIm\fR occurrences and
709 {\fIm\fR,\fIn\fR} matches any number of occurrences between \fIm\fR and
710 \fIn\fR, inclusive.
714 For example, in the string \fBabababccccccd\fR the ERE c{3} is matched by
715 characters seven to nine and the ERE (ab){2,} is matched by characters one to
716 six.
719 The behavior of multiple adjacent duplication symbols \fB(\fR+\fB,\fR \fB*\fR,
720 \fB?\fR and intervals) produces undefined results.
721 .SS "ERE Alternation"
724 Two EREs separated by the special character vertical-line (|) match a string
725 that is matched by either. For example, the ERE a((bc)|d) matches the string
726 abc and the string ad. Single characters, or expressions matching single
727 characters, separated by the vertical bar and enclosed in parentheses, will be
728 treated as an ERE matching a single character.
729 .SS "ERE Precedence"
732 The order of precedence will be as shown in the following table:
737 box;
738 l l
739 l l .
740 \fBERE Precedence (from high to low)\fR
741 collation-related bracket symbols       [= =]  [: :]  [. .]
742 escaped characters      \e<\fIspecial character\fR>
743 bracket expression      [ ]
744 grouping        ( )
745 single-character-ERE duplication        * + ? {\fIm\fR,\fIn\fR}
746 concatenation   
747 anchoring       ^  $
748 alternation     |
753 For example, the ERE \fBabba\||\|cde\fR matches either the string \fBabba\fR or
754 the string \fBcde\fR (rather than the string \fBabbade\fR or \fBabbcde\fR,
755 because concatenation has a higher order of precedence than alternation).
756 .SS "ERE Expression Anchoring"
759 An ERE can be limited to matching strings that begin or end a line; this is
760 called \fIanchoring\fR. The circumflex and dollar sign special characters are
761 considered ERE anchors when used anywhere outside a bracket expression. This
762 has the following effects:
763 .RS +4
766 A circumflex (^) outside a bracket expression anchors the expression or
767 subexpression it begins to the beginning of a string; such an expression or
768 subexpression can match only a sequence starting at the first character of a
769 string. For example, the EREs ^ab and (^ab) match ab in the string abcdef, but
770 fail to match in the string cdefab, and the ERE a^b is valid, but can never
771 match because the \fBa\fR prevents the expression ^b from matching starting at
772 the first character.
774 .RS +4
777 A dollar sign \fB(\fR \fB$\fR \fB)\fR outside a bracket expression anchors
778 the expression or subexpression it ends to the end of a string; such an
779 expression or subexpression can match only a sequence ending at the last
780 character of a string. For example, the EREs ef$ and (ef$) match ef in the
781 string abcdef, but fail to match in the string cdefab, and the ERE e$f is
782 valid, but can never match because the \fBf\fR prevents the expression e$ from
783 matching ending at the last character.
785 .SH SEE ALSO
788 \fBlocaledef\fR(1), \fBregcomp\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5),
789 \fBlocale\fR(5), \fBregexp\fR(5)