1regex(5) Standards, Environments, and Macros regex(5)
2
3
4
6 regex - internationalized basic and extended regular expression match‐
7 ing
8
10 Regular Expressions (REs) provide a mechanism to select specific
11 strings from a set of character strings. The Internationalized Regular
12 Expressions described below differ from the Simple Regular Expressions
13 described on the regexp(5) manual page in the following ways:
14
15 o both Basic and Extended Regular Expressions are supported
16
17 o the Internationalization features—character class, equiva‐
18 lence class, and multi-character collation—are supported.
19
20
21 The Basic Regular Expression (BRE) notation and construction rules
22 described in the BASIC REGULAR EXPRESSIONS section apply to most utili‐
23 ties supporting regular expressions. Some utilities, instead, support
24 the Extended Regular Expressions (ERE) described in the EXTENDED REGU‐
25 LAR EXPRESSIONS section; any exceptions for both cases are noted in the
26 descriptions of the specific utilities using regular expressions. Both
27 BREs and EREs are supported by the Regular Expression Matching inter‐
28 faces regcomp(3C) and regexec(3C).
29
31 BREs Matching a Single Character
32 A BRE ordinary character, a special character preceded by a backslash,
33 or a period matches a single character. A bracket expression matches a
34 single character or a single collating element. See RE Bracket Expres‐
35 sion, below.
36
37 BRE Ordinary Characters
38 An ordinary character is a BRE that matches itself: any character in
39 the supported character set, except for the BRE special characters
40 listed in BRE Special Characters, below.
41
42
43 The interpretation of an ordinary character preceded by a backslash (\)
44 is undefined, except for:
45
46 1. the characters ), (, {, and }
47
48 2. the digits 1 to 9 inclusive (see BREs Matching Multiple
49 Characters, below)
50
51 3. a character inside a bracket expression.
52
53 BRE Special Characters
54 A BRE special character has special properties in certain contexts.
55 Outside those contexts, or when preceded by a backslash, such a charac‐
56 ter will be a BRE that matches the special character itself. The BRE
57 special characters and the contexts in which they have their special
58 meaning are:
59
60 . [ \ The period, left-bracket, and backslash are special except
61 when used in a bracket expression (see RE Bracket Expres‐
62 sion, below). An expression containing a [ that is not pre‐
63 ceded by a backslash and is not part of a bracket expres‐
64 sion produces undefined results.
65
66
67 * The asterisk is special except when used:
68
69 o in a bracket expression
70
71 o as the first character of an entire BRE (after
72 an initial ^, if any)
73
74 o as the first character of a subexpression (after
75 an initial ^, if any); see BREs Matching Multi‐
76 ple Characters, below.
77
78
79 ^ The circumflex is special when used:
80
81 o as an anchor (see BRE Expression Anchoring,
82 below).
83
84 o as the first character of a bracket expression
85 (see RE Bracket Expression, below).
86
87
88 $ The dollar sign is special when used as an anchor.
89
90
91 Periods in BREs
92 A period (.), when used outside a bracket expression, is a BRE that
93 matches any character in the supported character set except NUL.
94
95 RE Bracket Expression
96 A bracket expression (an expression enclosed in square brackets, []) is
97 an RE that matches a single collating element contained in the non-
98 empty set of collating elements represented by the bracket expression.
99
100
101 The following rules and definitions apply to bracket expressions:
102
103 1. A bracket expression is either a matching list expression or
104 a non-matching list expression. It consists of one or more
105 expressions: collating elements, collating symbols, equiva‐
106 lence classes, character classes, or range expressions (see
107 rule 7 below). Portable applications must not use range
108 expressions, even though all implementations support them.
109 The right-bracket (]) loses its special meaning and repre‐
110 sents itself in a bracket expression if it occurs first in
111 the list (after an initial circumflex (^), if any). Other‐
112 wise, it terminates the bracket expression, unless it
113 appears in a collating symbol (such as [.].]) or is the end‐
114 ing right-bracket for a collating symbol, equivalence class,
115 or character class. The special characters:
116
117 . * [ \
118
119
120 (period, asterisk, left-bracket and backslash, respectively)
121 lose their special meaning within a bracket expression.
122
123 The character sequences:
124
125 [. [= [:
126
127
128 (left-bracket followed by a period, equals-sign, or colon)
129 are special inside a bracket expression and are used to
130 delimit collating symbols, equivalence class expressions,
131 and character class expressions. These symbols must be fol‐
132 lowed by a valid expression and the matching terminating
133 sequence .], =] or :], as described in the following items.
134
135 2. A matching list expression specifies a list that matches any
136 one of the expressions represented in the list. The first
137 character in the list must not be the circumflex. For exam‐
138 ple, [abc] is an RE that matches any of the characters a, b
139 or c.
140
141 3. A non-matching list expression begins with a circumflex (^),
142 and specifies a list that matches any character or collating
143 element except for the expressions represented in the list
144 after the leading circumflex. For example, [^abc] is an RE
145 that matches any character or collating element except the
146 characters a, b, or c. The circumflex will have this special
147 meaning only when it occurs first in the list, immediately
148 following the left-bracket.
149
150 4. A collating symbol is a collating element enclosed within
151 bracket-period ([..]) delimiters. Multi-character collating
152 elements must be represented as collating symbols when it is
153 necessary to distinguish them from a list of the individual
154 characters that make up the multi-character collating ele‐
155 ment. For example, if the string ch is a collating element
156 in the current collation sequence with the associated col‐
157 lating symbol <ch>, the expression [[.ch.]] will be treated
158 as an RE matching the character sequence ch, while [ch] will
159 be treated as an RE matching c or h. Collating symbols will
160 be recognized only inside bracket expressions. This implies
161 that the RE [[.ch.]]*c matches the first to fifth character
162 in the string chchch. If the string is not a collating ele‐
163 ment in the current collating sequence definition, or if the
164 collating element has no characters associated with it, the
165 symbol will be treated as an invalid expression.
166
167 5. An equivalence class expression represents the set of col‐
168 lating elements belonging to an equivalence class. Only pri‐
169 mary equivalence classes will be recognised. The class is
170 expressed by enclosing any one of the collating elements in
171 the equivalence class within bracket-equal ([==]) delim‐
172 iters. For example, if a and b belong to the same equiva‐
173 lence class, then [[=a=]b], [[==]b] and [[==]b] will each be
174 equivalent to [ab]. If the collating element does not belong
175 to an equivalence class, the equivalence class expression
176 will be treated as a collating symbol.
177
178 6. A character class expression represents the set of charac‐
179 ters belonging to a character class, as defined in the
180 LC_CTYPE category in the current locale. All character
181 classes specified in the current locale will be recognized.
182 A character class expression is expressed as a character
183 class name enclosed within bracket-colon ([::]) delimiters.
184
185 The following character class expressions are supported in
186 all locales:
187
188
189
190
191 [:alnum:] [:cntrl:] [:lower:] [:space:]
192 [:alpha:] [:digit:] [:print:] [:upper:]
193 [:blank:] [:graph:] [:punct:] [:xdigit:]
194
195 In addition, character class expressions of the form:
196
197 [:name:]
198
199
200 are recognized in those locales where the name keyword has
201 been given a charclass definition in the LC_CTYPE category.
202
203 7. A range expression represents the set of collating elements
204 that fall between two elements in the current collation
205 sequence, inclusively. It is expressed as the starting point
206 and the ending point separated by a hyphen (-).
207
208 Range expressions must not be used in portable applications
209 because their behavior is dependent on the collating
210 sequence. Ranges will be treated according to the current
211 collating sequence, and include such characters that fall
212 within the range based on that collating sequence, regard‐
213 less of character values. This, however, means that the
214 interpretation will differ depending on collating sequence.
215 If, for instance, one collating sequence defines as a vari‐
216 ant of a, while another defines it as a letter following z,
217 then the expression [-z] is valid in the first language and
218 invalid in the second.
219
220 In the following, all examples assume the collation sequence
221 specified for the POSIX locale, unless another collation
222 sequence is specifically defined.
223
224 The starting range point and the ending range point must be
225 a collating element or collating symbol. An equivalence
226 class expression used as a starting or ending point of a
227 range expression produces unspecified results. An equiva‐
228 lence class can be used portably within a bracket expres‐
229 sion, but only outside the range. For example, the unspeci‐
230 fied expression [[=e=]−f] should be given as [[=e=]e−f]. The
231 ending range point must collate equal to or higher than the
232 starting range point; otherwise, the expression will be
233 treated as invalid. The order used is the order in which the
234 collating elements are specified in the current collation
235 definition. One-to-many mappings (see locale(5)) will not be
236 performed. For example, assuming that the character eszet is
237 placed in the collation sequence after r and s, but before
238 t, and that it maps to the sequence ss for collation pur‐
239 poses, then the expression [r−s] matches only r and s, but
240 the expression [s−t] matches s, beta, or t.
241
242 The interpretation of range expressions where the ending
243 range point is also the starting range point of a subsequent
244 range expression (for instance [a−m−o]) is undefined.
245
246 The hyphen character will be treated as itself if it occurs
247 first (after an initial ^, if any) or last in the list, or
248 as an ending range point in a range expression. As examples,
249 the expressions [−ac] and [ac−] are equivalent and match any
250 of the characters a, c, or −; [^−ac] and [^ac−] are equiva‐
251 lent and match any characters except a, c, or −; the expres‐
252 sion [%−−] matches any of the characters between % and −
253 inclusive; the expression [−−@] matches any of the charac‐
254 ters between − and @ inclusive; and the expression [a−−@] is
255 invalid, because the letter a follows the symbol − in the
256 POSIX locale. To use a hyphen as the starting range point,
257 it must either come first in the bracket expression or be
258 specified as a collating symbol, for example: [][.−.]−0],
259 which matches either a right bracket or any character or
260 collating element that collates between hyphen and 0, inclu‐
261 sive.
262
263 If a bracket expression must specify both − and ], the ]
264 must be placed first (after the ^, if any) and the − last
265 within the bracket expression.
266
267
268 Note: Latin-1 characters such as ` or ^ are not printable in some
269 locales, for example, the ja locale.
270
271 BREs Matching Multiple Characters
272 The following rules can be used to construct BREs matching multiple
273 characters from BREs matching a single character:
274
275 1. The concatenation of BREs matches the concatenation of the
276 strings matched by each component of the BRE.
277
278 2. A subexpression can be defined within a BRE by enclosing it
279 between the character pairs \( and \) . Such a subexpression
280 matches whatever it would have matched without the \( and
281 \), except that anchoring within subexpressions is optional
282 behavior; see BRE Expression Anchoring, below. Subexpres‐
283 sions can be arbitrarily nested.
284
285 3. The back-reference expression \n matches the same (possibly
286 empty) string of characters as was matched by a subexpres‐
287 sion enclosed between \( and \) preceding the \n. The char‐
288 acter n must be a digit from 1 to 9 inclusive, nth subex‐
289 pression (the one that begins with the nth \( and ends with
290 the corresponding paired \)). The expression is invalid if
291 less than n subexpressions precede the \n. For example, the
292 expression ^\(.*\)\1$ matches a line consisting of two adja‐
293 cent appearances of the same string, and the expression
294 \(a\)*\1 fails to match a. The limit of nine back-references
295 to subexpressions in the RE is based on the use of a single
296 digit identifier. This does not imply that only nine subex‐
297 pressions are allowed in REs. The following is a valid BRE
298 with ten subexpressions:
299
300 \(\(\(ab\)*c\)*d\)\(ef\)*\(gh\)\{2\}\(ij\)*\(kl\)*\(mn\)*\(op\)*\(qr\)*
301
302
303
304 4. When a BRE matching a single character, a subexpression or a
305 back-reference is followed by the special character asterisk
306 (*), together with that asterisk it matches what zero or
307 more consecutive occurrences of the BRE would match. For
308 example, [ab]* and [ab][ab] are equivalent when matching the
309 string ab.
310
311 5. When a BRE matching a single character, a subexpression, or
312 a back-reference is followed by an interval expression of
313 the format \{m\}, \{m,\} or \{m,n\}, together with that
314 interval expression it matches what repeated consecutive
315 occurrences of the BRE would match. The values of m and n
316 will be decimal integers in the range 0 ≤ m ≤ n ≤
317 {RE_DUP_MAX}, where m specifies the exact or minimum number
318 of occurrences and n specifies the maximum number of occur‐
319 rences. The expression \{m\} matches exactly m occurrences
320 of the preceding BRE, \{m,\} matches at least m occurrences
321 and \{m,n\} matches any number of occurrences between m and
322 n, inclusive.
323
324 For example, in the string abababccccccd, the BRE c\{3\} is
325 matched by characters seven to nine, the BRE \(ab\)\{4,\} is
326 not matched at all and the BRE c\{1,3\}d is matched by char‐
327 acters ten to thirteen.
328
329
330 The behavior of multiple adjacent duplication symbols ( * and inter‐
331 vals) produces undefined results.
332
333 BRE Precedence
334 The order of precedence is as shown in the following table:
335
336
337
338
339
340
341
342
343 ┌────────────────────────────────────────────────────────────────┐
344 │BRE Precedence (from high to low) │
345 │collation-related bracket symbols [= =] [: :] [. .] │
346 │escaped characters \<special character> │
347 │bracket expression [ ] │
348 │subexpressions/back-references \( \) \n │
349 │single-character-BRE duplication * \{m,n\} │
350 │concatenation │
351 │anchoring ^ $ │
352 └────────────────────────────────────────────────────────────────┘
353
354 BRE Expression Anchoring
355 A BRE can be limited to matching strings that begin or end a line; this
356 is called anchoring. The circumflex and dollar sign special characters
357 will be considered BRE anchors in the following contexts:
358
359 1. A circumflex ( ^ ) is an anchor when used as the first char‐
360 acter of an entire BRE. The implementation may treat circum‐
361 flex as an anchor when used as the first character of a sub‐
362 expression. The circumflex will anchor the expression to the
363 beginning of a string; only sequences starting at the first
364 character of a string will be matched by the BRE. For exam‐
365 ple, the BRE ^ab matches ab in the string abcdef, but fails
366 to match in the string cdefab. A portable BRE must escape a
367 leading circumflex in a subexpression to match a literal
368 circumflex.
369
370 2. A dollar sign ( $ ) is an anchor when used as the last char‐
371 acter of an entire BRE. The implementation may treat a dol‐
372 lar sign as an anchor when used as the last character of a
373 subexpression. The dollar sign will anchor the expression to
374 the end of the string being matched; the dollar sign can be
375 said to match the end-of-string following the last charac‐
376 ter.
377
378 3. A BRE anchored by both ^ and $ matches only an entire
379 string. For example, the BRE ^abcdef$ matches strings con‐
380 sisting only of abcdef.
381
382 4. ^ and $ are not special in subexpressions.
383
384
385 Note: The Solaris implementation does not support anchoring in BRE sub‐
386 expressions.
387
389 The rules specififed for BREs apply to Extended Regular Expressions
390 (EREs) with the following exceptions:
391
392 o The characters |, +, and ? have special meaning, as defined
393 below.
394
395 o The { and } characters, when used as the duplication opera‐
396 tor, are not preceded by backslashes. The constructs \{ and
397 \} simply match the characters { and }, respectively.
398
399 o The back reference operator is not supported.
400
401 o Anchoring (^$) is supported in subexpressions.
402
403 EREs Matching a Single Character
404 An ERE ordinary character, a special character preceded by a backslash,
405 or a period matches a single character. A bracket expression matches a
406 single character or a single collating element. An ERE matching a sin‐
407 gle character enclosed in parentheses matches the same as the ERE with‐
408 out parentheses would have matched.
409
410 ERE Ordinary Characters
411 An ordinary character is an ERE that matches itself. An ordinary char‐
412 acter is any character in the supported character set, except for the
413 ERE special characters listed in ERE Special Characters below. The
414 interpretation of an ordinary character preceded by a backslash (\) is
415 undefined.
416
417 ERE Special Characters
418 An ERE special character has special properties in certain contexts.
419 Outside those contexts, or when preceded by a backslash, such a charac‐
420 ter is an ERE that matches the special character itself. The extended
421 regular expression special characters and the contexts in which they
422 have their special meaning are:
423
424 . [ \ ( The period, left-bracket, backslash, and left-parenthesis
425 are special except when used in a bracket expression (see
426 RE Bracket Expression, above). Outside a bracket expres‐
427 sion, a left-parenthesis immediately followed by a right-
428 parenthesis produces undefined results.
429
430
431 ) The right-parenthesis is special when matched with a pre‐
432 ceding left-parenthesis, both outside a bracket expres‐
433 sion.
434
435
436 * + ? { The asterisk, plus-sign, question-mark, and left-brace
437 are special except when used in a bracket expression (see
438 RE Bracket Expression, above). Any of the following uses
439 produce undefined results:
440
441 o if these characters appear first in an ERE, or
442 immediately following a vertical-line, circum‐
443 flex or left-parenthesis
444
445 o if a left-brace is not part of a valid inter‐
446 val expression.
447
448
449 | The vertical-line is special except when used in a
450 bracket expression (see RE Bracket Expression, above). A
451 vertical-line appearing first or last in an ERE, or imme‐
452 diately following a vertical-line or a left-parenthesis,
453 or immediately preceding a right-parenthesis, produces
454 undefined results.
455
456
457 ^ The circumflex is special when used:
458
459 o as an anchor (see ERE Expression Anchoring,
460 below).
461
462 o as the first character of a bracket expression
463 (see RE Bracket Expression, above).
464
465
466 $ The dollar sign is special when used as an anchor.
467
468
469 Periods in EREs
470 A period (.), when used outside a bracket expression, is an ERE that
471 matches any character in the supported character set except NUL.
472
473 ERE Bracket Expression
474 The rules for ERE Bracket Expressions are the same as for Basic Regular
475 Expressions; see RE Bracket Expression, above).
476
477 EREs Matching Multiple Characters
478 The following rules will be used to construct EREs matching multiple
479 characters from EREs matching a single character:
480
481 1. A concatenation of EREs matches the concatenation of the
482 character sequences matched by each component of the ERE. A
483 concatenation of EREs enclosed in parentheses matches what‐
484 ever the concatenation without the parentheses matches. For
485 example, both the ERE cd and the ERE (cd) are matched by the
486 third and fourth character of the string abcdefabcdef.
487
488 2. When an ERE matching a single character or an ERE enclosed
489 in parentheses is followed by the special character plus-
490 sign (+), together with that plus-sign it matches what one
491 or more consecutive occurrences of the ERE would match. For
492 example, the ERE b+(bc) matches the fourth to seventh char‐
493 acters in the string acabbbcde; [ab] + and [ab][ab]* are
494 equivalent.
495
496 3. When an ERE matching a single character or an ERE enclosed
497 in parentheses is followed by the special character asterisk
498 (*), together with that asterisk it matches what zero or
499 more consecutive occurrences of the ERE would match. For
500 example, the ERE b*c matches the first character in the
501 string cabbbcde, and the ERE b*cd matches the third to sev‐
502 enth characters in the string cabbbcdebbbbbbcdbc. And, [ab]*
503 and [ab][ab] are equivalent when matching the string ab.
504
505 4. When an ERE matching a single character or an ERE enclosed
506 in parentheses is followed by the special character ques‐
507 tion-mark (?), together with that question-mark it matches
508 what zero or one consecutive occurrences of the ERE would
509 match. For example, the ERE b?c matches the second character
510 in the string acabbbcde.
511
512 5. When an ERE matching a single character or an ERE enclosed
513 in parentheses is followed by an interval expression of the
514 format {m}, {m,} or {m,n}, together with that interval
515 expression it matches what repeated consecutive occurrences
516 of the ERE would match. The values of m and n will be deci‐
517 mal integers in the range 0 ≤ m ≤ n ≤ {RE_DUP_MAX}, where m
518 specifies the exact or minimum number of occurrences and n
519 specifies the maximum number of occurrences. The expression
520 {m} matches exactly m occurrences of the preceding ERE, {m,}
521 matches at least m occurrences and {m,n} matches any number
522 of occurrences between m and n, inclusive.
523
524
525 For example, in the string abababccccccd the ERE c{3} is matched by
526 characters seven to nine and the ERE (ab){2,} is matched by characters
527 one to six.
528
529
530 The behavior of multiple adjacent duplication symbols (+, *, ? and
531 intervals) produces undefined results.
532
533 ERE Alternation
534 Two EREs separated by the special character vertical-line (|) match a
535 string that is matched by either. For example, the ERE a((bc)|d)
536 matches the string abc and the string ad. Single characters, or expres‐
537 sions matching single characters, separated by the vertical bar and
538 enclosed in parentheses, will be treated as an ERE matching a single
539 character.
540
541 ERE Precedence
542 The order of precedence will be as shown in the following table:
543
544
545
546
547 ┌────────────────────────────────────────────────────────────────┐
548 │ERE Precedence (from high to low) │
549 │collation-related bracket symbols [= =] [: :] [. .] │
550 │escaped characters \<special character> │
551 │bracket expression [ ] │
552 │grouping ( ) │
553 │single-character-ERE duplication * + ? {m,n} │
554 │concatenation │
555 │anchoring ^ $ │
556 │alternation | │
557 └────────────────────────────────────────────────────────────────┘
558
559
560 For example, the ERE abba|cde matches either the string abba or the
561 string cde (rather than the string abbade or abbcde, because concatena‐
562 tion has a higher order of precedence than alternation).
563
564 ERE Expression Anchoring
565 An ERE can be limited to matching strings that begin or end a line;
566 this is called anchoring. The circumflex and dollar sign special char‐
567 acters are considered ERE anchors when used anywhere outside a bracket
568 expression. This has the following effects:
569
570 1. A circumflex (^) outside a bracket expression anchors the
571 expression or subexpression it begins to the beginning of a
572 string; such an expression or subexpression can match only a
573 sequence starting at the first character of a string. For
574 example, the EREs ^ab and (^ab) match ab in the string
575 abcdef, but fail to match in the string cdefab, and the ERE
576 a^b is valid, but can never match because the a prevents the
577 expression ^b from matching starting at the first character.
578
579 2. A dollar sign ( $ ) outside a bracket expression anchors the
580 expression or subexpression it ends to the end of a string;
581 such an expression or subexpression can match only a
582 sequence ending at the last character of a string. For exam‐
583 ple, the EREs ef$ and (ef$) match ef in the string abcdef,
584 but fail to match in the string cdefab, and the ERE e$f is
585 valid, but can never match because the f prevents the
586 expression e$ from matching ending at the last character.
587
589 localedef(1), regcomp(3C), attributes(5), environ(5), locale(5), reg‐
590 exp(5)
591
592
593
594SunOS 5.11 21 Apr 2005 regex(5)