summaryrefslogtreecommitdiff
path: root/usr/src/man/man5/regex.5
blob: ae69ddd34f0e765114669aaa53362d017a50e405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
'\" te
.\" Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
.\" Portions Copyright (c) 1999, Sun Microsystems, Inc.  All Rights Reserved
.\" 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
.\" http://www.opengroup.org/bookstore/.
.\" 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.
.\"  This notice shall appear on any product containing this material.
.\" 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.
.\" 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.
.\" 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]
.TH REGEX 5 "Apr 21, 2005"
.SH NAME
regex \- internationalized basic and extended regular expression matching
.SH DESCRIPTION
.sp
.LP
Regular Expressions (REs) provide a mechanism to select specific strings from a
set of character strings. The Internationalized Regular Expressions described
below differ from the Simple Regular Expressions described on the
\fBregexp\fR(5) manual page in the following ways:
.RS +4
.TP
.ie t \(bu
.el o
both Basic and Extended Regular Expressions are supported
.RE
.RS +4
.TP
.ie t \(bu
.el o
the Internationalization features\(emcharacter class, equivalence class, and
multi-character collation\(emare supported.
.RE
.sp
.LP
The Basic Regular Expression (BRE) notation and construction rules described in
the \fBBASIC\fR \fBREGULAR\fR \fBEXPRESSIONS\fR section apply to most utilities
supporting regular expressions. Some utilities, instead, support the Extended
Regular Expressions (ERE) described in the \fBEXTENDED\fR \fBREGULAR\fR
\fBEXPRESSIONS\fR section; any exceptions for both cases are noted in the
descriptions of the specific utilities using regular expressions. Both BREs and
EREs are supported by the Regular Expression Matching interfaces
\fBregcomp\fR(3C) and \fBregexec\fR(3C).
.SH BASIC REGULAR EXPRESSIONS
.SS "BREs Matching a Single Character"
.sp
.LP
A BRE ordinary character, a special character preceded by a backslash, or a
period matches a single character. A bracket expression matches a single
character or a single collating element. See \fBRE Bracket Expression\fR,
below.
.SS "BRE Ordinary Characters"
.sp
.LP
An ordinary character is a BRE that matches itself: any character in the
supported character set, except for the BRE special characters listed in \fBBRE
Special Characters\fR, below.
.sp
.LP
The interpretation of an ordinary character preceded by a backslash (\e) is
undefined, except for:
.RS +4
.TP
1.
the characters ), (, {, and }
.RE
.RS +4
.TP
2.
the digits 1 to 9 inclusive (see \fBBREs Matching Multiple Characters\fR,
below)
.RE
.RS +4
.TP
3.
a character inside a bracket expression.
.RE
.SS "BRE Special Characters"
.sp
.LP
A BRE \fIspecial\fR \fIcharacter\fR has special properties in certain contexts.
Outside those contexts, or when preceded by a backslash, such a character will
be a BRE that matches the special character itself. The BRE special characters
and the contexts in which they have their special meaning are:
.sp
.ne 2
.na
\fB\&. [ \e\fR
.ad
.RS 12n
The period, left-bracket, and backslash are special except when used in a
bracket expression (see \fBRE Bracket Expression\fR, below). An expression
containing a [ that is not preceded by a backslash and is not part of a bracket
expression produces undefined results.
.RE

.sp
.ne 2
.na
\fB*\fR
.ad
.RS 12n
The asterisk is special except when used:
.RS +4
.TP
.ie t \(bu
.el o
in a bracket expression
.RE
.RS +4
.TP
.ie t \(bu
.el o
as the first character of an entire BRE (after an initial ^, if any)
.RE
.RS +4
.TP
.ie t \(bu
.el o
as the first character of a subexpression (after an initial ^, if any); see
\fBBREs Matching Multiple Characters\fR, below.
.RE
.RE

.sp
.ne 2
.na
\fB\fI^\fR\fR
.ad
.RS 12n
The circumflex is special when used:
.RS +4
.TP
.ie t \(bu
.el o
as an anchor (see \fBBRE Expression Anchoring\fR, below).
.RE
.RS +4
.TP
.ie t \(bu
.el o
as the first character of a bracket expression (see \fBRE Bracket
Expression\fR, below).
.RE
.RE

.sp
.ne 2
.na
\fB$\fR
.ad
.RS 12n
The dollar sign is special when used as an anchor.
.RE

.SS "Periods in BREs"
.sp
.LP
A period (\fB\&.\fR), when used outside a bracket expression, is a BRE that
matches any character in the supported character set except NUL.
.SS "RE Bracket Expression"
.sp
.LP
A bracket expression (an expression enclosed in square brackets, [\|]) is an RE
that matches a single collating element contained in the non-empty set of
collating elements represented by the bracket expression.
.sp
.LP
The following rules and definitions apply to bracket expressions:
.RS +4
.TP
1.
A \fIbracket expression\fR is either a matching list expression or a
non-matching list expression. It consists of one or more expressions: collating
elements, collating symbols, equivalence classes, character classes, or range
expressions (see rule 7 below). Portable applications must not use range
expressions, even though all implementations support them. The right-bracket
(]) loses its special meaning and represents itself in a bracket expression if
it occurs first in the list (after an initial circumflex (^), if any).
Otherwise, it terminates the bracket expression, unless it appears in a
collating symbol (such as [.].]) or is the ending right-bracket for a collating
symbol, equivalence class, or character class. The special characters:
.sp
.in +2
.nf
	.   *   [   \e
.fi
.in -2
.sp

(period, asterisk, left-bracket and backslash, respectively) lose their special
meaning within a bracket expression.
.sp
The character sequences:
.sp
.in +2
.nf
	[.   [=    [:
.fi
.in -2
.sp

(left-bracket followed by a period, equals-sign, or colon) are special inside a
bracket expression and are used to delimit collating symbols, equivalence class
expressions, and character class expressions. These symbols must be followed by
a valid expression and the matching terminating sequence .], =] or :], as
described in the following items.
.RE
.RS +4
.TP
2.
A \fImatching list\fR expression specifies a list that matches any one of
the expressions represented in the list. The first character in the list must
not be the circumflex. For example, \fB[abc]\fR is an RE that matches any of
the characters \fBa\fR, \fBb\fR or \fBc\fR.
.RE
.RS +4
.TP
3.
A \fInon-matching list\fR expression begins with a circumflex (^), and
specifies a list that matches any character or collating element except for the
expressions represented in the list after the leading circumflex. For example,
[^abc] is an RE that matches any character or collating element except the
characters \fBa,\fR \fBb\fR, or \fBc\fR. The circumflex will have this special
meaning only when it occurs first in the list, immediately following the
left-bracket.
.RE
.RS +4
.TP
4.
A \fIcollating symbol\fR is a collating element enclosed within
bracket-period ([..]) delimiters. Multi-character collating elements must be
represented as collating symbols when it is necessary to distinguish them from
a list of the individual characters that make up the multi-character collating
element. For example, if the string \fBch\fR is a collating element in the
current collation sequence with the associated collating symbol <ch>, the
expression [[.ch.]] will be treated as an RE matching the character sequence
\fBch\fR, while [ch] will be treated as an RE matching \fBc\fR or \fBh\fR.
Collating symbols will be recognized only inside bracket expressions. This
implies that the RE \fB[[.ch.]]*c\fR matches the first to fifth character in
the string chchch. If the string is not a collating element in the current
collating sequence definition, or if the collating element has no characters
associated with it, the symbol will be treated as an invalid expression.
.RE
.RS +4
.TP
5.
An \fIequivalence class expression\fR represents the set of collating
elements belonging to an equivalence class. Only primary equivalence classes
will be recognised. The class is expressed by enclosing any one of the
collating elements in the equivalence class within bracket-equal ([==])
delimiters. For example, if a and b belong to the same equivalence class, then
[[=a=]b], [[==]b] and [[==]b] will each be equivalent to [ab]. If the collating
element does not belong to an equivalence class, the equivalence class
expression will be treated as a \fIcollating symbol\fR.
.RE
.RS +4
.TP
6.
A \fIcharacter class expression\fR represents the set of characters
belonging to a character class, as defined in the \fBLC_CTYPE\fR category in
the current locale. All character classes specified in the current locale will
be recognized. A character class expression is expressed as a character class
name enclosed within bracket-colon ([::]) delimiters.
.sp
The following character class expressions are supported in all locales:
.sp

.sp
.TS
l l l l
l l l l .
\fB[:alnum:]\fR	\fB[:cntrl:]\fR	\fB[:lower:]\fR	\fB[:space:]\fR
\fB[:alpha:]\fR	\fB[:digit:]\fR	\fB[:print:]\fR	\fB[:upper:]\fR
\fB[:blank:]\fR	\fB[:graph:]\fR	\fB[:punct:]\fR	\fB[:xdigit:]\fR
.TE

In addition, character class expressions of the form:
.sp
.in +2
.nf
	\fB[:name:]\fR
.fi
.in -2
.sp

are recognized in those locales where the \fIname\fR keyword has been given a
\fBcharclass\fR definition in the \fBLC_CTYPE\fR category.
.RE
.RS +4
.TP
7.
A \fIrange expression\fR represents the set of collating elements that fall
between two elements in the current collation sequence, inclusively. It is
expressed as the starting point and the ending point separated by a hyphen
(\fB-\fR).
.sp
Range expressions must not be used in portable applications because their
behavior is dependent on the collating sequence. Ranges will be treated
according to the current collating sequence, and include such characters that
fall within the range based on that collating sequence, regardless of character
values. This, however, means that the interpretation will differ depending on
collating sequence. If, for instance, one collating sequence defines as a
variant of \fBa\fR, while another defines it as a letter following \fBz\fR,
then the expression [-z] is valid in the first language and invalid in the
second.
.sp
In the following, all examples assume the collation sequence specified for the
POSIX locale, unless another collation sequence is specifically defined.
.sp
The starting range point and the ending range point must be a collating element
or collating symbol. An equivalence class expression used as a starting or
ending point of a range expression produces unspecified results. An equivalence
class can be used portably within a bracket expression, but only outside the
range. For example, the unspecified expression [[=e=]\(mif] should be given as
[[=e=]e\(mif]. The ending range point must collate equal to or higher than the
starting range point; otherwise, the expression will be treated as invalid. The
order used is the order in which the collating elements are specified in the
current collation definition. One-to-many mappings (see \fBlocale\fR(5)) will
not be performed. For example, assuming that the character eszet is placed in
the collation sequence after \fBr\fR and \fBs\fR, but before \fBt\fR, and that
it maps to the sequence \fBss\fR for collation purposes, then the expression
[r\(mis] matches only \fBr\fR and \fBs\fR, but the expression [s\(mit] matches
\fBs\fR, \fBbeta\fR, or \fBt\fR.
.sp
The interpretation of range expressions where the ending range point is also
the starting range point of a subsequent range expression (for instance
[a\(mim\(mio]) is undefined.
.sp
The hyphen character will be treated as itself if it occurs first (after an
initial ^, if any) or last in the list, or as an ending range point in a range
expression. As examples, the expressions [\(miac] and [ac\(mi] are equivalent
and match any of the characters \fBa\fR, \fBc\fR, or \fB\(mi;\fR [^\(miac] and
[^ac\(mi] are equivalent and match any characters except \fBa\fR, \fBc\fR, or
\(mi; the expression [%\(mi\|\(mi] matches any of the characters between % and
\(mi inclusive; the expression [\(mi\|\(mi@] matches any of the characters
between \(mi and @ inclusive; and the expression [a\(mi\|\(mi@] is invalid,
because the letter \fBa\fR follows the symbol \(mi in the POSIX locale. To use
a hyphen as the starting range point, it must either come first in the bracket
expression or be specified as a collating symbol, for example:
[][.\(mi.]\(mi0], which matches either a right bracket or any character or
collating element that collates between hyphen and 0, inclusive.
.sp
If a bracket expression must specify both \(mi and ], the ] must be placed
first (after the ^, if any) and the \(mi last within the bracket expression.
.RE
.sp
.LP
Note: Latin-1 characters such as \(ga or ^ are not printable in some locales,
for example, the \fBja\fR locale.
.SS "BREs Matching Multiple Characters"
.sp
.LP
The following rules can be used to construct BREs matching multiple characters
from BREs matching a single character:
.RS +4
.TP
1.
The concatenation of BREs matches the concatenation of the strings matched
by each component of the BRE.
.RE
.RS +4
.TP
2.
A \fIsubexpression\fR can be defined within a BRE by enclosing it between
the character pairs \e( and \e) . Such a subexpression matches whatever it
would have matched without the \e( and \e), except that anchoring within
subexpressions is optional behavior; see \fBBRE Expression Anchoring\fR, below.
Subexpressions can be arbitrarily nested.
.RE
.RS +4
.TP
3.
The \fIback-reference\fR expression \e\fIn\fR matches the same (possibly
empty) string of characters as was matched by a subexpression enclosed between
\e( and \e) preceding the \e\fIn\fR. The character \fIn\fR must be a digit from
1 to 9 inclusive, \fIn\fRth subexpression (the one that begins with the
\fIn\fRth \e( and ends with the corresponding paired \e)\|). The expression is
invalid if less than \fIn\fR subexpressions precede the \e\fIn\fR. For example,
the expression \|^\e(.*\e)\e1$ matches a line consisting of two adjacent
appearances of the same string, and the expression \e(a\e)*\e1 fails to match
\fBa\fR. The limit of nine back-references to subexpressions in the RE is based
on the use of a single digit identifier. This does not imply that only nine
subexpressions are allowed in REs. The following is a valid BRE with ten
subexpressions:
.sp
.in +2
.nf
\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
.fi
.in -2
.sp

.RE
.RS +4
.TP
4.
When a BRE matching a single character, a subexpression or a back-reference
is followed by the special character asterisk (*), together with that asterisk
it matches what zero or more consecutive occurrences of the BRE would match.
For example, \fB[ab]*\fR \fBand\fR \fB[ab][ab]\fR are equivalent when matching
the string \fBab\fR.
.RE
.RS +4
.TP
5.
When a BRE matching a single character, a subexpression, or a back-reference
is followed by an \fIinterval expression\fR of the format \e{\fIm\fR\e},
\e{\fIm\fR,\e} or \e{\fIm\fR,\fIn\fR\e}, together with that interval expression
it matches what repeated consecutive occurrences of the BRE would match. The
values of \fIm\fR and \fIn\fR will be decimal integers in the range 0 \(<=
\fIm\fR \(<= \fIn\fR \(<= {\fBRE_DUP_MAX\fR}, where \fIm\fR specifies the exact
or minimum number of occurrences and \fIn\fR specifies the maximum number of
occurrences. The expression \e{\fIm\fR\e} matches exactly \fIm\fR occurrences
of the preceding BRE, \e{\fIm\fR,\e} matches at least \fIm\fR occurrences and
\e{\fIm,n\fR\e} matches any number of occurrences between \fIm\fR and \fIn\fR,
inclusive.
.sp
For example, in the string \fBabababccccccd\fR, the BRE c\e{3\e} is matched by
characters seven to nine, the BRE \e(ab\e)\e{4,\e} is not matched at all and
the BRE c\e{1,3\e}d is matched by characters ten to thirteen.
.RE
.sp
.LP
The behavior of multiple adjacent duplication symbols \fB(\fR \fB*\fR \fB\fR
and intervals) produces undefined results.
.SS "BRE Precedence"
.sp
.LP
The order of precedence is as shown in the following table:
.sp
.in +2
.nf

.fi
.in -2
.sp

.sp

.sp
.TS
box;
l l
l l .
\fBBRE Precedence (from high to low)\fR
collation-related bracket symbols	[= =]  [: :]  [. .]
escaped characters	\e<\fIspecial character\fR>
bracket expression	[ ]
subexpressions/back-references	\e( \e) \e\fIn\fR
single-character-BRE duplication	* \e{\fIm\fR,\fIn\fR\e}
concatenation	
anchoring	^  $
.TE

.SS "BRE Expression Anchoring"
.sp
.LP
A BRE can be limited to matching strings that begin or end a line; this is
called \fIanchoring\fR. The circumflex and dollar sign special characters will
be considered BRE anchors in the following contexts:
.RS +4
.TP
1.
A circumflex \fB(\fR \fB^\fR \fB)\fR is an anchor when used as the first
character of an entire BRE. The implementation may treat circumflex as an
anchor when used as the first character of a subexpression. The circumflex will
anchor the expression to the beginning of a string; only sequences starting at
the first character of a string will be matched by the BRE. For example, the
BRE ^ab matches \fBab\fR in the string \fBabcdef,\fR but fails to match in the
string \fBcdefab\fR. A portable BRE must escape a leading circumflex in a
subexpression to match a literal circumflex.
.RE
.RS +4
.TP
2.
A dollar sign \fB(\fR \fB$\fR \fB)\fR is an anchor when used as the last
character of an entire BRE. The implementation may treat a dollar sign as an
anchor when used as the last character of a subexpression. The dollar sign will
anchor the expression to the end of the string being matched; the dollar sign
can be said to match the end-of-string following the last character.
.RE
.RS +4
.TP
3.
A BRE anchored by both \fB^\fR and \fB$\fR matches only an entire string.
For example, the BRE \fB^abcdef$\fR matches strings consisting only of
\fBabcdef\fR.
.RE
.RS +4
.TP
4.
\fB^\fR and \fB$\fR are not special in subexpressions.
.RE
.sp
.LP
Note: The Solaris implementation does not support anchoring in BRE
subexpressions.
.SH EXTENDED REGULAR EXPRESSIONS
.sp
.LP
The rules specififed for BREs apply to Extended Regular Expressions (EREs) with
the following exceptions:
.RS +4
.TP
.ie t \(bu
.el o
The characters \fB|\fR, \fB+\fR, and \fB?\fR have special meaning, as defined
below.
.RE
.RS +4
.TP
.ie t \(bu
.el o
The \fB{\fR and \fB}\fR characters, when used as the duplication operator, are
not preceded by backslashes. The constructs \fB\e{\fR and \fB\e}\fR simply
match the characters \fB{\fR and \fB}\fR, respectively.
.RE
.RS +4
.TP
.ie t \(bu
.el o
The back reference operator is not supported.
.RE
.RS +4
.TP
.ie t \(bu
.el o
Anchoring (\fB^$\fR) is supported in subexpressions.
.RE
.SS "EREs Matching a Single Character"
.sp
.LP
An ERE ordinary character, a special character preceded by a backslash, or a
period matches a single character. A bracket expression matches a single
character or a single collating element. An \fIERE\fR \fImatching\fR \fIa\fR
\fIsingle\fR \fIcharacter\fR enclosed in parentheses matches the same as the
ERE without parentheses would have matched.
.SS "ERE Ordinary Characters"
.sp
.LP
An \fIordinary character\fR is an ERE that matches itself. An ordinary
character is any character in the supported character set, except for the ERE
special characters listed in \fBERE\fR \fBSpecial\fR \fBCharacters\fR below.
The interpretation of an ordinary character preceded by a backslash (\fB\e\fR)
is undefined.
.SS "ERE Special Characters"
.sp
.LP
An \fIERE\fR \fIspecial\fR \fIcharacter\fR has special properties in certain
contexts. Outside those contexts, or when preceded by a backslash, such a
character is an ERE that matches the special character itself. The extended
regular expression special characters and the contexts in which they have their
special meaning are:
.sp
.ne 2
.na
\fB\&. [ \e (\fR
.ad
.RS 14n
The period, left-bracket, backslash, and left-parenthesis are special except
when used in a bracket expression (see \fBRE Bracket Expression\fR, above).
Outside a bracket expression, a left-parenthesis immediately followed by a
right-parenthesis produces undefined results.
.RE

.sp
.ne 2
.na
\fB)\fR
.ad
.RS 14n
The right-parenthesis is special when matched with a preceding
left-parenthesis, both outside a bracket expression.
.RE

.sp
.ne 2
.na
\fB* + ? {\fR
.ad
.RS 14n
The asterisk, plus-sign, question-mark, and left-brace are special except when
used in a bracket expression (see \fBRE Bracket Expression\fR, above). Any of
the following uses produce undefined results:
.RS +4
.TP
.ie t \(bu
.el o
if these characters appear first in an ERE, or immediately following a
vertical-line, circumflex or left-parenthesis
.RE
.RS +4
.TP
.ie t \(bu
.el o
if a left-brace is not part of a valid interval expression.
.RE
.RE

.sp
.ne 2
.na
\fB|\fR
.ad
.RS 14n
The vertical-line is special except when used in a bracket expression (see
\fBRE Bracket Expression\fR, above). A vertical-line appearing first or last in
an ERE, or immediately following a vertical-line or a left-parenthesis, or
immediately preceding a right-parenthesis, produces undefined results.
.RE

.sp
.ne 2
.na
\fB^\fR
.ad
.RS 14n
The circumflex is special when used:
.RS +4
.TP
.ie t \(bu
.el o
as an anchor (see \fBERE Expression Anchoring\fR, below).
.RE
.RS +4
.TP
.ie t \(bu
.el o
as the first character of a bracket expression (see \fBRE Bracket
Expression\fR, above).
.RE
.RE

.sp
.ne 2
.na
\fB$\fR
.ad
.RS 14n
The dollar sign is special when used as an anchor.
.RE

.SS "Periods in EREs"
.sp
.LP
A period (\fB\&.\fR), when used outside a bracket expression, is an ERE that
matches any character in the supported character set except NUL.
.SS "ERE Bracket Expression"
.sp
.LP
The rules for ERE Bracket Expressions are the same as for Basic Regular
Expressions; see \fBRE Bracket Expression\fR, above).
.SS "EREs Matching Multiple Characters"
.sp
.LP
The following rules will be used to construct EREs matching multiple characters
from EREs matching a single character:
.RS +4
.TP
1.
A \fIconcatenation\fR \fIof\fR \fIEREs\fR matches the concatenation of the
character sequences matched by each component of the ERE. A concatenation of
EREs enclosed in parentheses matches whatever the concatenation without the
parentheses matches. For example, both the ERE \fBcd\fR and the ERE \fB(cd)\fR
are matched by the third and fourth character of the string \fBabcdefabcdef\fR.
.RE
.RS +4
.TP
2.
When an ERE matching a single character or an ERE enclosed in parentheses is
followed by the special character plus-sign (+), together with that plus-sign
it matches what one or more consecutive occurrences of the ERE would match. For
example, the ERE \fBb+(bc)\fR matches the fourth to seventh characters in the
string \fBacabbbcde;\fR \fB[ab]\fR + and \fB[ab][ab]*\fR are equivalent.
.RE
.RS +4
.TP
3.
When an ERE matching a single character or an ERE enclosed in parentheses is
followed by the special character asterisk (\fB*\fR), together with that
asterisk it matches what zero or more consecutive occurrences of the ERE would
match. For example, the ERE \fBb*c\fR matches the first character in the string
\fBcabbbcde,\fR and the ERE \fBb*cd\fR matches the third to seventh characters
in the string \fBcabbbcdebbbbbbcdbc\fR. And, \fB[ab]*\fR and \fB[ab][ab]\fR are
equivalent when matching the string \fBab\fR.
.RE
.RS +4
.TP
4.
When an ERE matching a single character or an ERE enclosed in parentheses is
followed by the special character question-mark (\fB?\fR), together with that
question-mark it matches what zero or one consecutive occurrences of the ERE
would match. For example, the ERE \fBb?c\fR matches the second character in the
string \fBacabbbcde\fR.
.RE
.RS +4
.TP
5.
When an ERE matching a single character or an ERE enclosed in parentheses is
followed by an \fIinterval\fR \fIexpression\fR of the format {\fIm\fR},
{\fIm\fR,} or {\fIm\fR,\fIn\fR}, together with that interval expression it
matches what repeated consecutive occurrences of the ERE would match. The
values of \fIm\fR and \fIn\fR will be decimal integers in the range 0 \(<=
\fIm\fR \(<= \fIn\fR \(<= \fB{RE_DUP_MAX}\fR, where \fIm\fR specifies the exact
or minimum number of occurrences and \fIn\fR specifies the maximum number of
occurrences. The expression {\fIm\fR} matches exactly \fIm\fR occurrences of
the preceding ERE, {\fIm\fR,} matches at least \fIm\fR occurrences and
{\fIm\fR,\fIn\fR} matches any number of occurrences between \fIm\fR and
\fIn\fR, inclusive.
.RE
.sp
.LP
For example, in the string \fBabababccccccd\fR the ERE c{3} is matched by
characters seven to nine and the ERE (ab){2,} is matched by characters one to
six.
.sp
.LP
The behavior of multiple adjacent duplication symbols \fB(\fR+\fB,\fR \fB*\fR,
\fB?\fR and intervals) produces undefined results.
.SS "ERE Alternation"
.sp
.LP
Two EREs separated by the special character vertical-line (|) match a string
that is matched by either. For example, the ERE a((bc)|d) matches the string
abc and the string ad. Single characters, or expressions matching single
characters, separated by the vertical bar and enclosed in parentheses, will be
treated as an ERE matching a single character.
.SS "ERE Precedence"
.sp
.LP
The order of precedence will be as shown in the following table:
.sp

.sp
.TS
box;
l l
l l .
\fBERE Precedence (from high to low)\fR
collation-related bracket symbols	[= =]  [: :]  [. .]
escaped characters	\e<\fIspecial character\fR>
bracket expression	[ ]
grouping	( )
single-character-ERE duplication	* + ? {\fIm\fR,\fIn\fR}
concatenation	
anchoring	^  $
alternation	|
.TE

.sp
.LP
For example, the ERE \fBabba\||\|cde\fR matches either the string \fBabba\fR or
the string \fBcde\fR (rather than the string \fBabbade\fR or \fBabbcde\fR,
because concatenation has a higher order of precedence than alternation).
.SS "ERE Expression Anchoring"
.sp
.LP
An ERE can be limited to matching strings that begin or end a line; this is
called \fIanchoring\fR. The circumflex and dollar sign special characters are
considered ERE anchors when used anywhere outside a bracket expression. This
has the following effects:
.RS +4
.TP
1.
A circumflex (^) outside a bracket expression anchors the expression or
subexpression it begins to the beginning of a string; such an expression or
subexpression can match only a sequence starting at the first character of a
string. For example, the EREs ^ab and (^ab) match ab in the string abcdef, but
fail to match in the string cdefab, and the ERE a^b is valid, but can never
match because the \fBa\fR prevents the expression ^b from matching starting at
the first character.
.RE
.RS +4
.TP
2.
A dollar sign \fB(\fR \fB$\fR \fB)\fR outside a bracket expression anchors
the expression or subexpression it ends to the end of a string; such an
expression or subexpression can match only a sequence ending at the last
character of a string. For example, the EREs ef$ and (ef$) match ef in the
string abcdef, but fail to match in the string cdefab, and the ERE e$f is
valid, but can never match because the \fBf\fR prevents the expression e$ from
matching ending at the last character.
.RE
.SH SEE ALSO
.sp
.LP
\fBlocaledef\fR(1), \fBregcomp\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5),
\fBlocale\fR(5), \fBregexp\fR(5)