]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/regex/regex.3
Update to zstd 1.3.2
[FreeBSD/FreeBSD.git] / lib / libc / regex / regex.3
1 .\" Copyright (c) 1992, 1993, 1994 Henry Spencer.
2 .\" Copyright (c) 1992, 1993, 1994
3 .\"     The Regents of the University of California.  All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Henry Spencer.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)regex.3     8.4 (Berkeley) 3/20/94
33 .\" $FreeBSD$
34 .\"
35 .Dd May 25, 2016
36 .Dt REGEX 3
37 .Os
38 .Sh NAME
39 .Nm regcomp ,
40 .Nm regexec ,
41 .Nm regerror ,
42 .Nm regfree
43 .Nd regular-expression library
44 .Sh LIBRARY
45 .Lb libc
46 .Sh SYNOPSIS
47 .In regex.h
48 .Ft int
49 .Fo regcomp
50 .Fa "regex_t * restrict preg" "const char * restrict pattern" "int cflags"
51 .Fc
52 .Ft int
53 .Fo regexec
54 .Fa "const regex_t * restrict preg" "const char * restrict string"
55 .Fa "size_t nmatch" "regmatch_t pmatch[restrict]" "int eflags"
56 .Fc
57 .Ft size_t
58 .Fo regerror
59 .Fa "int errcode" "const regex_t * restrict preg"
60 .Fa "char * restrict errbuf" "size_t errbuf_size"
61 .Fc
62 .Ft void
63 .Fn regfree "regex_t *preg"
64 .Sh DESCRIPTION
65 These routines implement
66 .St -p1003.2
67 regular expressions
68 .Pq Do RE Dc Ns s ;
69 see
70 .Xr re_format 7 .
71 The
72 .Fn regcomp
73 function
74 compiles an RE written as a string into an internal form,
75 .Fn regexec
76 matches that internal form against a string and reports results,
77 .Fn regerror
78 transforms error codes from either into human-readable messages,
79 and
80 .Fn regfree
81 frees any dynamically-allocated storage used by the internal form
82 of an RE.
83 .Pp
84 The header
85 .In regex.h
86 declares two structure types,
87 .Ft regex_t
88 and
89 .Ft regmatch_t ,
90 the former for compiled internal forms and the latter for match reporting.
91 It also declares the four functions,
92 a type
93 .Ft regoff_t ,
94 and a number of constants with names starting with
95 .Dq Dv REG_ .
96 .Pp
97 The
98 .Fn regcomp
99 function
100 compiles the regular expression contained in the
101 .Fa pattern
102 string,
103 subject to the flags in
104 .Fa cflags ,
105 and places the results in the
106 .Ft regex_t
107 structure pointed to by
108 .Fa preg .
109 The
110 .Fa cflags
111 argument
112 is the bitwise OR of zero or more of the following flags:
113 .Bl -tag -width REG_EXTENDED
114 .It Dv REG_EXTENDED
115 Compile modern
116 .Pq Dq extended
117 REs,
118 rather than the obsolete
119 .Pq Dq basic
120 REs that
121 are the default.
122 .It Dv REG_BASIC
123 This is a synonym for 0,
124 provided as a counterpart to
125 .Dv REG_EXTENDED
126 to improve readability.
127 .It Dv REG_NOSPEC
128 Compile with recognition of all special characters turned off.
129 All characters are thus considered ordinary,
130 so the
131 .Dq RE
132 is a literal string.
133 This is an extension,
134 compatible with but not specified by
135 .St -p1003.2 ,
136 and should be used with
137 caution in software intended to be portable to other systems.
138 .Dv REG_EXTENDED
139 and
140 .Dv REG_NOSPEC
141 may not be used
142 in the same call to
143 .Fn regcomp .
144 .It Dv REG_ICASE
145 Compile for matching that ignores upper/lower case distinctions.
146 See
147 .Xr re_format 7 .
148 .It Dv REG_NOSUB
149 Compile for matching that need only report success or failure,
150 not what was matched.
151 .It Dv REG_NEWLINE
152 Compile for newline-sensitive matching.
153 By default, newline is a completely ordinary character with no special
154 meaning in either REs or strings.
155 With this flag,
156 .Ql [^
157 bracket expressions and
158 .Ql .\&
159 never match newline,
160 a
161 .Ql ^\&
162 anchor matches the null string after any newline in the string
163 in addition to its normal function,
164 and the
165 .Ql $\&
166 anchor matches the null string before any newline in the
167 string in addition to its normal function.
168 .It Dv REG_PEND
169 The regular expression ends,
170 not at the first NUL,
171 but just before the character pointed to by the
172 .Va re_endp
173 member of the structure pointed to by
174 .Fa preg .
175 The
176 .Va re_endp
177 member is of type
178 .Ft "const char *" .
179 This flag permits inclusion of NULs in the RE;
180 they are considered ordinary characters.
181 This is an extension,
182 compatible with but not specified by
183 .St -p1003.2 ,
184 and should be used with
185 caution in software intended to be portable to other systems.
186 .El
187 .Pp
188 When successful,
189 .Fn regcomp
190 returns 0 and fills in the structure pointed to by
191 .Fa preg .
192 One member of that structure
193 (other than
194 .Va re_endp )
195 is publicized:
196 .Va re_nsub ,
197 of type
198 .Ft size_t ,
199 contains the number of parenthesized subexpressions within the RE
200 (except that the value of this member is undefined if the
201 .Dv REG_NOSUB
202 flag was used).
203 If
204 .Fn regcomp
205 fails, it returns a non-zero error code;
206 see
207 .Sx DIAGNOSTICS .
208 .Pp
209 The
210 .Fn regexec
211 function
212 matches the compiled RE pointed to by
213 .Fa preg
214 against the
215 .Fa string ,
216 subject to the flags in
217 .Fa eflags ,
218 and reports results using
219 .Fa nmatch ,
220 .Fa pmatch ,
221 and the returned value.
222 The RE must have been compiled by a previous invocation of
223 .Fn regcomp .
224 The compiled form is not altered during execution of
225 .Fn regexec ,
226 so a single compiled RE can be used simultaneously by multiple threads.
227 .Pp
228 By default,
229 the NUL-terminated string pointed to by
230 .Fa string
231 is considered to be the text of an entire line, minus any terminating
232 newline.
233 The
234 .Fa eflags
235 argument is the bitwise OR of zero or more of the following flags:
236 .Bl -tag -width REG_STARTEND
237 .It Dv REG_NOTBOL
238 The first character of the string is treated as the continuation
239 of a line.
240 This means that the anchors
241 .Ql ^\& ,
242 .Ql [[:<:]] ,
243 and
244 .Ql \e<
245 do not match before it; but see
246 .Dv REG_STARTEND
247 below.
248 This does not affect the behavior of newlines under
249 .Dv REG_NEWLINE .
250 .It Dv REG_NOTEOL
251 The NUL terminating
252 the string
253 does not end a line, so the
254 .Ql $\&
255 anchor does not match before it.
256 This does not affect the behavior of newlines under
257 .Dv REG_NEWLINE .
258 .It Dv REG_STARTEND
259 The string is considered to start at
260 .Fa string No +
261 .Fa pmatch Ns [0]. Ns Fa rm_so
262 and to end before the byte located at
263 .Fa string No +
264 .Fa pmatch Ns [0]. Ns Fa rm_eo ,
265 regardless of the value of
266 .Fa nmatch .
267 See below for the definition of
268 .Fa pmatch
269 and
270 .Fa nmatch .
271 This is an extension,
272 compatible with but not specified by
273 .St -p1003.2 ,
274 and should be used with
275 caution in software intended to be portable to other systems.
276 .Pp
277 Without
278 .Dv REG_NOTBOL ,
279 the position
280 .Fa rm_so
281 is considered the beginning of a line, such that
282 .Ql ^
283 matches before it, and the beginning of a word if there is a word
284 character at this position, such that
285 .Ql [[:<:]]
286 and
287 .Ql \e<
288 match before it.
289 .Pp
290 With
291 .Dv REG_NOTBOL ,
292 the character at position
293 .Fa rm_so
294 is treated as the continuation of a line, and if
295 .Fa rm_so
296 is greater than 0, the preceding character is taken into consideration.
297 If the preceding character is a newline and the regular expression was compiled
298 with
299 .Dv REG_NEWLINE ,
300 .Ql ^
301 matches before the string; if the preceding character is not a word character
302 but the string starts with a word character,
303 .Ql [[:<:]]
304 and
305 .Ql \e<
306 match before the string.
307 .El
308 .Pp
309 See
310 .Xr re_format 7
311 for a discussion of what is matched in situations where an RE or a
312 portion thereof could match any of several substrings of
313 .Fa string .
314 .Pp
315 Normally,
316 .Fn regexec
317 returns 0 for success and the non-zero code
318 .Dv REG_NOMATCH
319 for failure.
320 Other non-zero error codes may be returned in exceptional situations;
321 see
322 .Sx DIAGNOSTICS .
323 .Pp
324 If
325 .Dv REG_NOSUB
326 was specified in the compilation of the RE,
327 or if
328 .Fa nmatch
329 is 0,
330 .Fn regexec
331 ignores the
332 .Fa pmatch
333 argument (but see below for the case where
334 .Dv REG_STARTEND
335 is specified).
336 Otherwise,
337 .Fa pmatch
338 points to an array of
339 .Fa nmatch
340 structures of type
341 .Ft regmatch_t .
342 Such a structure has at least the members
343 .Va rm_so
344 and
345 .Va rm_eo ,
346 both of type
347 .Ft regoff_t
348 (a signed arithmetic type at least as large as an
349 .Ft off_t
350 and a
351 .Ft ssize_t ) ,
352 containing respectively the offset of the first character of a substring
353 and the offset of the first character after the end of the substring.
354 Offsets are measured from the beginning of the
355 .Fa string
356 argument given to
357 .Fn regexec .
358 An empty substring is denoted by equal offsets,
359 both indicating the character following the empty substring.
360 .Pp
361 The 0th member of the
362 .Fa pmatch
363 array is filled in to indicate what substring of
364 .Fa string
365 was matched by the entire RE.
366 Remaining members report what substring was matched by parenthesized
367 subexpressions within the RE;
368 member
369 .Va i
370 reports subexpression
371 .Va i ,
372 with subexpressions counted (starting at 1) by the order of their opening
373 parentheses in the RE, left to right.
374 Unused entries in the array (corresponding either to subexpressions that
375 did not participate in the match at all, or to subexpressions that do not
376 exist in the RE (that is,
377 .Va i
378 >
379 .Fa preg Ns -> Ns Va re_nsub ) )
380 have both
381 .Va rm_so
382 and
383 .Va rm_eo
384 set to -1.
385 If a subexpression participated in the match several times,
386 the reported substring is the last one it matched.
387 (Note, as an example in particular, that when the RE
388 .Ql "(b*)+"
389 matches
390 .Ql bbb ,
391 the parenthesized subexpression matches each of the three
392 .So Li b Sc Ns s
393 and then
394 an infinite number of empty strings following the last
395 .Ql b ,
396 so the reported substring is one of the empties.)
397 .Pp
398 If
399 .Dv REG_STARTEND
400 is specified,
401 .Fa pmatch
402 must point to at least one
403 .Ft regmatch_t
404 (even if
405 .Fa nmatch
406 is 0 or
407 .Dv REG_NOSUB
408 was specified),
409 to hold the input offsets for
410 .Dv REG_STARTEND .
411 Use for output is still entirely controlled by
412 .Fa nmatch ;
413 if
414 .Fa nmatch
415 is 0 or
416 .Dv REG_NOSUB
417 was specified,
418 the value of
419 .Fa pmatch Ns [0]
420 will not be changed by a successful
421 .Fn regexec .
422 .Pp
423 The
424 .Fn regerror
425 function
426 maps a non-zero
427 .Fa errcode
428 from either
429 .Fn regcomp
430 or
431 .Fn regexec
432 to a human-readable, printable message.
433 If
434 .Fa preg
435 is
436 .No non\- Ns Dv NULL ,
437 the error code should have arisen from use of
438 the
439 .Ft regex_t
440 pointed to by
441 .Fa preg ,
442 and if the error code came from
443 .Fn regcomp ,
444 it should have been the result from the most recent
445 .Fn regcomp
446 using that
447 .Ft regex_t .
448 The
449 .Po
450 .Fn regerror
451 may be able to supply a more detailed message using information
452 from the
453 .Ft regex_t .
454 .Pc
455 The
456 .Fn regerror
457 function
458 places the NUL-terminated message into the buffer pointed to by
459 .Fa errbuf ,
460 limiting the length (including the NUL) to at most
461 .Fa errbuf_size
462 bytes.
463 If the whole message will not fit,
464 as much of it as will fit before the terminating NUL is supplied.
465 In any case,
466 the returned value is the size of buffer needed to hold the whole
467 message (including terminating NUL).
468 If
469 .Fa errbuf_size
470 is 0,
471 .Fa errbuf
472 is ignored but the return value is still correct.
473 .Pp
474 If the
475 .Fa errcode
476 given to
477 .Fn regerror
478 is first ORed with
479 .Dv REG_ITOA ,
480 the
481 .Dq message
482 that results is the printable name of the error code,
483 e.g.\&
484 .Dq Dv REG_NOMATCH ,
485 rather than an explanation thereof.
486 If
487 .Fa errcode
488 is
489 .Dv REG_ATOI ,
490 then
491 .Fa preg
492 shall be
493 .No non\- Ns Dv NULL
494 and the
495 .Va re_endp
496 member of the structure it points to
497 must point to the printable name of an error code;
498 in this case, the result in
499 .Fa errbuf
500 is the decimal digits of
501 the numeric value of the error code
502 (0 if the name is not recognized).
503 .Dv REG_ITOA
504 and
505 .Dv REG_ATOI
506 are intended primarily as debugging facilities;
507 they are extensions,
508 compatible with but not specified by
509 .St -p1003.2 ,
510 and should be used with
511 caution in software intended to be portable to other systems.
512 Be warned also that they are considered experimental and changes are possible.
513 .Pp
514 The
515 .Fn regfree
516 function
517 frees any dynamically-allocated storage associated with the compiled RE
518 pointed to by
519 .Fa preg .
520 The remaining
521 .Ft regex_t
522 is no longer a valid compiled RE
523 and the effect of supplying it to
524 .Fn regexec
525 or
526 .Fn regerror
527 is undefined.
528 .Pp
529 None of these functions references global variables except for tables
530 of constants;
531 all are safe for use from multiple threads if the arguments are safe.
532 .Sh IMPLEMENTATION CHOICES
533 There are a number of decisions that
534 .St -p1003.2
535 leaves up to the implementor,
536 either by explicitly saying
537 .Dq undefined
538 or by virtue of them being
539 forbidden by the RE grammar.
540 This implementation treats them as follows.
541 .Pp
542 See
543 .Xr re_format 7
544 for a discussion of the definition of case-independent matching.
545 .Pp
546 There is no particular limit on the length of REs,
547 except insofar as memory is limited.
548 Memory usage is approximately linear in RE size, and largely insensitive
549 to RE complexity, except for bounded repetitions.
550 See
551 .Sx BUGS
552 for one short RE using them
553 that will run almost any system out of memory.
554 .Pp
555 A backslashed character other than one specifically given a magic meaning
556 by
557 .St -p1003.2
558 (such magic meanings occur only in obsolete
559 .Bq Dq basic
560 REs)
561 is taken as an ordinary character.
562 .Pp
563 Any unmatched
564 .Ql [\&
565 is a
566 .Dv REG_EBRACK
567 error.
568 .Pp
569 Equivalence classes cannot begin or end bracket-expression ranges.
570 The endpoint of one range cannot begin another.
571 .Pp
572 .Dv RE_DUP_MAX ,
573 the limit on repetition counts in bounded repetitions, is 255.
574 .Pp
575 A repetition operator
576 .Ql ( ?\& ,
577 .Ql *\& ,
578 .Ql +\& ,
579 or bounds)
580 cannot follow another
581 repetition operator.
582 A repetition operator cannot begin an expression or subexpression
583 or follow
584 .Ql ^\&
585 or
586 .Ql |\& .
587 .Pp
588 .Ql |\&
589 cannot appear first or last in a (sub)expression or after another
590 .Ql |\& ,
591 i.e., an operand of
592 .Ql |\&
593 cannot be an empty subexpression.
594 An empty parenthesized subexpression,
595 .Ql "()" ,
596 is legal and matches an
597 empty (sub)string.
598 An empty string is not a legal RE.
599 .Pp
600 A
601 .Ql {\&
602 followed by a digit is considered the beginning of bounds for a
603 bounded repetition, which must then follow the syntax for bounds.
604 A
605 .Ql {\&
606 .Em not
607 followed by a digit is considered an ordinary character.
608 .Pp
609 .Ql ^\&
610 and
611 .Ql $\&
612 beginning and ending subexpressions in obsolete
613 .Pq Dq basic
614 REs are anchors, not ordinary characters.
615 .Sh DIAGNOSTICS
616 Non-zero error codes from
617 .Fn regcomp
618 and
619 .Fn regexec
620 include the following:
621 .Pp
622 .Bl -tag -width REG_ECOLLATE -compact
623 .It Dv REG_NOMATCH
624 The
625 .Fn regexec
626 function
627 failed to match
628 .It Dv REG_BADPAT
629 invalid regular expression
630 .It Dv REG_ECOLLATE
631 invalid collating element
632 .It Dv REG_ECTYPE
633 invalid character class
634 .It Dv REG_EESCAPE
635 .Ql \e
636 applied to unescapable character
637 .It Dv REG_ESUBREG
638 invalid backreference number
639 .It Dv REG_EBRACK
640 brackets
641 .Ql "[ ]"
642 not balanced
643 .It Dv REG_EPAREN
644 parentheses
645 .Ql "( )"
646 not balanced
647 .It Dv REG_EBRACE
648 braces
649 .Ql "{ }"
650 not balanced
651 .It Dv REG_BADBR
652 invalid repetition count(s) in
653 .Ql "{ }"
654 .It Dv REG_ERANGE
655 invalid character range in
656 .Ql "[ ]"
657 .It Dv REG_ESPACE
658 ran out of memory
659 .It Dv REG_BADRPT
660 .Ql ?\& ,
661 .Ql *\& ,
662 or
663 .Ql +\&
664 operand invalid
665 .It Dv REG_EMPTY
666 empty (sub)expression
667 .It Dv REG_ASSERT
668 cannot happen - you found a bug
669 .It Dv REG_INVARG
670 invalid argument, e.g.\& negative-length string
671 .It Dv REG_ILLSEQ
672 illegal byte sequence (bad multibyte character)
673 .El
674 .Sh SEE ALSO
675 .Xr grep 1 ,
676 .Xr re_format 7
677 .Pp
678 .St -p1003.2 ,
679 sections 2.8 (Regular Expression Notation)
680 and
681 B.5 (C Binding for Regular Expression Matching).
682 .Sh HISTORY
683 Originally written by
684 .An Henry Spencer .
685 Altered for inclusion in the
686 .Bx 4.4
687 distribution.
688 .Sh BUGS
689 This is an alpha release with known defects.
690 Please report problems.
691 .Pp
692 The back-reference code is subtle and doubts linger about its correctness
693 in complex cases.
694 .Pp
695 The
696 .Fn regexec
697 function
698 performance is poor.
699 This will improve with later releases.
700 The
701 .Fa nmatch
702 argument
703 exceeding 0 is expensive;
704 .Fa nmatch
705 exceeding 1 is worse.
706 The
707 .Fn regexec
708 function
709 is largely insensitive to RE complexity
710 .Em except
711 that back
712 references are massively expensive.
713 RE length does matter; in particular, there is a strong speed bonus
714 for keeping RE length under about 30 characters,
715 with most special characters counting roughly double.
716 .Pp
717 The
718 .Fn regcomp
719 function
720 implements bounded repetitions by macro expansion,
721 which is costly in time and space if counts are large
722 or bounded repetitions are nested.
723 An RE like, say,
724 .Ql "((((a{1,100}){1,100}){1,100}){1,100}){1,100}"
725 will (eventually) run almost any existing machine out of swap space.
726 .Pp
727 There are suspected problems with response to obscure error conditions.
728 Notably,
729 certain kinds of internal overflow,
730 produced only by truly enormous REs or by multiply nested bounded repetitions,
731 are probably not handled well.
732 .Pp
733 Due to a mistake in
734 .St -p1003.2 ,
735 things like
736 .Ql "a)b"
737 are legal REs because
738 .Ql )\&
739 is
740 a special character only in the presence of a previous unmatched
741 .Ql (\& .
742 This cannot be fixed until the spec is fixed.
743 .Pp
744 The standard's definition of back references is vague.
745 For example, does
746 .Ql "a\e(\e(b\e)*\e2\e)*d"
747 match
748 .Ql "abbbd" ?
749 Until the standard is clarified,
750 behavior in such cases should not be relied on.
751 .Pp
752 The implementation of word-boundary matching is a bit of a kludge,
753 and bugs may lurk in combinations of word-boundary matching and anchoring.
754 .Pp
755 Word-boundary matching does not work properly in multibyte locales.