]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libc/regex/regex.3
Merge MAP_GUARD.
[FreeBSD/stable/10.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 .\" 4. 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 .Fn ( regerror
450 may be able to supply a more detailed message using information
451 from the
452 .Ft regex_t . )
453 The
454 .Fn regerror
455 function
456 places the NUL-terminated message into the buffer pointed to by
457 .Fa errbuf ,
458 limiting the length (including the NUL) to at most
459 .Fa errbuf_size
460 bytes.
461 If the whole message will not fit,
462 as much of it as will fit before the terminating NUL is supplied.
463 In any case,
464 the returned value is the size of buffer needed to hold the whole
465 message (including terminating NUL).
466 If
467 .Fa errbuf_size
468 is 0,
469 .Fa errbuf
470 is ignored but the return value is still correct.
471 .Pp
472 If the
473 .Fa errcode
474 given to
475 .Fn regerror
476 is first ORed with
477 .Dv REG_ITOA ,
478 the
479 .Dq message
480 that results is the printable name of the error code,
481 e.g.\&
482 .Dq Dv REG_NOMATCH ,
483 rather than an explanation thereof.
484 If
485 .Fa errcode
486 is
487 .Dv REG_ATOI ,
488 then
489 .Fa preg
490 shall be
491 .No non\- Ns Dv NULL
492 and the
493 .Va re_endp
494 member of the structure it points to
495 must point to the printable name of an error code;
496 in this case, the result in
497 .Fa errbuf
498 is the decimal digits of
499 the numeric value of the error code
500 (0 if the name is not recognized).
501 .Dv REG_ITOA
502 and
503 .Dv REG_ATOI
504 are intended primarily as debugging facilities;
505 they are extensions,
506 compatible with but not specified by
507 .St -p1003.2 ,
508 and should be used with
509 caution in software intended to be portable to other systems.
510 Be warned also that they are considered experimental and changes are possible.
511 .Pp
512 The
513 .Fn regfree
514 function
515 frees any dynamically-allocated storage associated with the compiled RE
516 pointed to by
517 .Fa preg .
518 The remaining
519 .Ft regex_t
520 is no longer a valid compiled RE
521 and the effect of supplying it to
522 .Fn regexec
523 or
524 .Fn regerror
525 is undefined.
526 .Pp
527 None of these functions references global variables except for tables
528 of constants;
529 all are safe for use from multiple threads if the arguments are safe.
530 .Sh IMPLEMENTATION CHOICES
531 There are a number of decisions that
532 .St -p1003.2
533 leaves up to the implementor,
534 either by explicitly saying
535 .Dq undefined
536 or by virtue of them being
537 forbidden by the RE grammar.
538 This implementation treats them as follows.
539 .Pp
540 See
541 .Xr re_format 7
542 for a discussion of the definition of case-independent matching.
543 .Pp
544 There is no particular limit on the length of REs,
545 except insofar as memory is limited.
546 Memory usage is approximately linear in RE size, and largely insensitive
547 to RE complexity, except for bounded repetitions.
548 See
549 .Sx BUGS
550 for one short RE using them
551 that will run almost any system out of memory.
552 .Pp
553 A backslashed character other than one specifically given a magic meaning
554 by
555 .St -p1003.2
556 (such magic meanings occur only in obsolete
557 .Bq Dq basic
558 REs)
559 is taken as an ordinary character.
560 .Pp
561 Any unmatched
562 .Ql [\&
563 is a
564 .Dv REG_EBRACK
565 error.
566 .Pp
567 Equivalence classes cannot begin or end bracket-expression ranges.
568 The endpoint of one range cannot begin another.
569 .Pp
570 .Dv RE_DUP_MAX ,
571 the limit on repetition counts in bounded repetitions, is 255.
572 .Pp
573 A repetition operator
574 .Ql ( ?\& ,
575 .Ql *\& ,
576 .Ql +\& ,
577 or bounds)
578 cannot follow another
579 repetition operator.
580 A repetition operator cannot begin an expression or subexpression
581 or follow
582 .Ql ^\&
583 or
584 .Ql |\& .
585 .Pp
586 .Ql |\&
587 cannot appear first or last in a (sub)expression or after another
588 .Ql |\& ,
589 i.e., an operand of
590 .Ql |\&
591 cannot be an empty subexpression.
592 An empty parenthesized subexpression,
593 .Ql "()" ,
594 is legal and matches an
595 empty (sub)string.
596 An empty string is not a legal RE.
597 .Pp
598 A
599 .Ql {\&
600 followed by a digit is considered the beginning of bounds for a
601 bounded repetition, which must then follow the syntax for bounds.
602 A
603 .Ql {\&
604 .Em not
605 followed by a digit is considered an ordinary character.
606 .Pp
607 .Ql ^\&
608 and
609 .Ql $\&
610 beginning and ending subexpressions in obsolete
611 .Pq Dq basic
612 REs are anchors, not ordinary characters.
613 .Sh DIAGNOSTICS
614 Non-zero error codes from
615 .Fn regcomp
616 and
617 .Fn regexec
618 include the following:
619 .Pp
620 .Bl -tag -width REG_ECOLLATE -compact
621 .It Dv REG_NOMATCH
622 The
623 .Fn regexec
624 function
625 failed to match
626 .It Dv REG_BADPAT
627 invalid regular expression
628 .It Dv REG_ECOLLATE
629 invalid collating element
630 .It Dv REG_ECTYPE
631 invalid character class
632 .It Dv REG_EESCAPE
633 .Ql \e
634 applied to unescapable character
635 .It Dv REG_ESUBREG
636 invalid backreference number
637 .It Dv REG_EBRACK
638 brackets
639 .Ql "[ ]"
640 not balanced
641 .It Dv REG_EPAREN
642 parentheses
643 .Ql "( )"
644 not balanced
645 .It Dv REG_EBRACE
646 braces
647 .Ql "{ }"
648 not balanced
649 .It Dv REG_BADBR
650 invalid repetition count(s) in
651 .Ql "{ }"
652 .It Dv REG_ERANGE
653 invalid character range in
654 .Ql "[ ]"
655 .It Dv REG_ESPACE
656 ran out of memory
657 .It Dv REG_BADRPT
658 .Ql ?\& ,
659 .Ql *\& ,
660 or
661 .Ql +\&
662 operand invalid
663 .It Dv REG_EMPTY
664 empty (sub)expression
665 .It Dv REG_ASSERT
666 cannot happen - you found a bug
667 .It Dv REG_INVARG
668 invalid argument, e.g.\& negative-length string
669 .It Dv REG_ILLSEQ
670 illegal byte sequence (bad multibyte character)
671 .El
672 .Sh SEE ALSO
673 .Xr grep 1 ,
674 .Xr re_format 7
675 .Pp
676 .St -p1003.2 ,
677 sections 2.8 (Regular Expression Notation)
678 and
679 B.5 (C Binding for Regular Expression Matching).
680 .Sh HISTORY
681 Originally written by
682 .An Henry Spencer .
683 Altered for inclusion in the
684 .Bx 4.4
685 distribution.
686 .Sh BUGS
687 This is an alpha release with known defects.
688 Please report problems.
689 .Pp
690 The back-reference code is subtle and doubts linger about its correctness
691 in complex cases.
692 .Pp
693 The
694 .Fn regexec
695 function
696 performance is poor.
697 This will improve with later releases.
698 The
699 .Fa nmatch
700 argument
701 exceeding 0 is expensive;
702 .Fa nmatch
703 exceeding 1 is worse.
704 The
705 .Fn regexec
706 function
707 is largely insensitive to RE complexity
708 .Em except
709 that back
710 references are massively expensive.
711 RE length does matter; in particular, there is a strong speed bonus
712 for keeping RE length under about 30 characters,
713 with most special characters counting roughly double.
714 .Pp
715 The
716 .Fn regcomp
717 function
718 implements bounded repetitions by macro expansion,
719 which is costly in time and space if counts are large
720 or bounded repetitions are nested.
721 An RE like, say,
722 .Ql "((((a{1,100}){1,100}){1,100}){1,100}){1,100}"
723 will (eventually) run almost any existing machine out of swap space.
724 .Pp
725 There are suspected problems with response to obscure error conditions.
726 Notably,
727 certain kinds of internal overflow,
728 produced only by truly enormous REs or by multiply nested bounded repetitions,
729 are probably not handled well.
730 .Pp
731 Due to a mistake in
732 .St -p1003.2 ,
733 things like
734 .Ql "a)b"
735 are legal REs because
736 .Ql )\&
737 is
738 a special character only in the presence of a previous unmatched
739 .Ql (\& .
740 This cannot be fixed until the spec is fixed.
741 .Pp
742 The standard's definition of back references is vague.
743 For example, does
744 .Ql "a\e(\e(b\e)*\e2\e)*d"
745 match
746 .Ql "abbbd" ?
747 Until the standard is clarified,
748 behavior in such cases should not be relied on.
749 .Pp
750 The implementation of word-boundary matching is a bit of a kludge,
751 and bugs may lurk in combinations of word-boundary matching and anchoring.
752 .Pp
753 Word-boundary matching does not work properly in multibyte locales.