]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/regex/regex.3
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 August 17, 2005
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
239 the string
240 is not the beginning of a line, so the
241 .Ql ^\&
242 anchor should not match before it.
243 This does not affect the behavior of newlines under
244 .Dv REG_NEWLINE .
245 .It Dv REG_NOTEOL
246 The NUL terminating
247 the string
248 does not end a line, so the
249 .Ql $\&
250 anchor should not match before it.
251 This does not affect the behavior of newlines under
252 .Dv REG_NEWLINE .
253 .It Dv REG_STARTEND
254 The string is considered to start at
255 .Fa string
256 +
257 .Fa pmatch Ns [0]. Ns Va rm_so
258 and to have a terminating NUL located at
259 .Fa string
260 +
261 .Fa pmatch Ns [0]. Ns Va rm_eo
262 (there need not actually be a NUL at that location),
263 regardless of the value of
264 .Fa nmatch .
265 See below for the definition of
266 .Fa pmatch
267 and
268 .Fa nmatch .
269 This is an extension,
270 compatible with but not specified by
271 .St -p1003.2 ,
272 and should be used with
273 caution in software intended to be portable to other systems.
274 Note that a non-zero
275 .Va rm_so
276 does not imply
277 .Dv REG_NOTBOL ;
278 .Dv REG_STARTEND
279 affects only the location of the string,
280 not how it is matched.
281 .El
282 .Pp
283 See
284 .Xr re_format 7
285 for a discussion of what is matched in situations where an RE or a
286 portion thereof could match any of several substrings of
287 .Fa string .
288 .Pp
289 Normally,
290 .Fn regexec
291 returns 0 for success and the non-zero code
292 .Dv REG_NOMATCH
293 for failure.
294 Other non-zero error codes may be returned in exceptional situations;
295 see
296 .Sx DIAGNOSTICS .
297 .Pp
298 If
299 .Dv REG_NOSUB
300 was specified in the compilation of the RE,
301 or if
302 .Fa nmatch
303 is 0,
304 .Fn regexec
305 ignores the
306 .Fa pmatch
307 argument (but see below for the case where
308 .Dv REG_STARTEND
309 is specified).
310 Otherwise,
311 .Fa pmatch
312 points to an array of
313 .Fa nmatch
314 structures of type
315 .Ft regmatch_t .
316 Such a structure has at least the members
317 .Va rm_so
318 and
319 .Va rm_eo ,
320 both of type
321 .Ft regoff_t
322 (a signed arithmetic type at least as large as an
323 .Ft off_t
324 and a
325 .Ft ssize_t ) ,
326 containing respectively the offset of the first character of a substring
327 and the offset of the first character after the end of the substring.
328 Offsets are measured from the beginning of the
329 .Fa string
330 argument given to
331 .Fn regexec .
332 An empty substring is denoted by equal offsets,
333 both indicating the character following the empty substring.
334 .Pp
335 The 0th member of the
336 .Fa pmatch
337 array is filled in to indicate what substring of
338 .Fa string
339 was matched by the entire RE.
340 Remaining members report what substring was matched by parenthesized
341 subexpressions within the RE;
342 member
343 .Va i
344 reports subexpression
345 .Va i ,
346 with subexpressions counted (starting at 1) by the order of their opening
347 parentheses in the RE, left to right.
348 Unused entries in the array (corresponding either to subexpressions that
349 did not participate in the match at all, or to subexpressions that do not
350 exist in the RE (that is,
351 .Va i
352 >
353 .Fa preg Ns -> Ns Va re_nsub ) )
354 have both
355 .Va rm_so
356 and
357 .Va rm_eo
358 set to -1.
359 If a subexpression participated in the match several times,
360 the reported substring is the last one it matched.
361 (Note, as an example in particular, that when the RE
362 .Ql "(b*)+"
363 matches
364 .Ql bbb ,
365 the parenthesized subexpression matches each of the three
366 .So Li b Sc Ns s
367 and then
368 an infinite number of empty strings following the last
369 .Ql b ,
370 so the reported substring is one of the empties.)
371 .Pp
372 If
373 .Dv REG_STARTEND
374 is specified,
375 .Fa pmatch
376 must point to at least one
377 .Ft regmatch_t
378 (even if
379 .Fa nmatch
380 is 0 or
381 .Dv REG_NOSUB
382 was specified),
383 to hold the input offsets for
384 .Dv REG_STARTEND .
385 Use for output is still entirely controlled by
386 .Fa nmatch ;
387 if
388 .Fa nmatch
389 is 0 or
390 .Dv REG_NOSUB
391 was specified,
392 the value of
393 .Fa pmatch Ns [0]
394 will not be changed by a successful
395 .Fn regexec .
396 .Pp
397 The
398 .Fn regerror
399 function
400 maps a non-zero
401 .Fa errcode
402 from either
403 .Fn regcomp
404 or
405 .Fn regexec
406 to a human-readable, printable message.
407 If
408 .Fa preg
409 is
410 .No non\- Ns Dv NULL ,
411 the error code should have arisen from use of
412 the
413 .Ft regex_t
414 pointed to by
415 .Fa preg ,
416 and if the error code came from
417 .Fn regcomp ,
418 it should have been the result from the most recent
419 .Fn regcomp
420 using that
421 .Ft regex_t .
422 The
423 .Fn ( regerror
424 may be able to supply a more detailed message using information
425 from the
426 .Ft regex_t . )
427 The
428 .Fn regerror
429 function
430 places the NUL-terminated message into the buffer pointed to by
431 .Fa errbuf ,
432 limiting the length (including the NUL) to at most
433 .Fa errbuf_size
434 bytes.
435 If the whole message will not fit,
436 as much of it as will fit before the terminating NUL is supplied.
437 In any case,
438 the returned value is the size of buffer needed to hold the whole
439 message (including terminating NUL).
440 If
441 .Fa errbuf_size
442 is 0,
443 .Fa errbuf
444 is ignored but the return value is still correct.
445 .Pp
446 If the
447 .Fa errcode
448 given to
449 .Fn regerror
450 is first ORed with
451 .Dv REG_ITOA ,
452 the
453 .Dq message
454 that results is the printable name of the error code,
455 e.g.\&
456 .Dq Dv REG_NOMATCH ,
457 rather than an explanation thereof.
458 If
459 .Fa errcode
460 is
461 .Dv REG_ATOI ,
462 then
463 .Fa preg
464 shall be
465 .No non\- Ns Dv NULL
466 and the
467 .Va re_endp
468 member of the structure it points to
469 must point to the printable name of an error code;
470 in this case, the result in
471 .Fa errbuf
472 is the decimal digits of
473 the numeric value of the error code
474 (0 if the name is not recognized).
475 .Dv REG_ITOA
476 and
477 .Dv REG_ATOI
478 are intended primarily as debugging facilities;
479 they are extensions,
480 compatible with but not specified by
481 .St -p1003.2 ,
482 and should be used with
483 caution in software intended to be portable to other systems.
484 Be warned also that they are considered experimental and changes are possible.
485 .Pp
486 The
487 .Fn regfree
488 function
489 frees any dynamically-allocated storage associated with the compiled RE
490 pointed to by
491 .Fa preg .
492 The remaining
493 .Ft regex_t
494 is no longer a valid compiled RE
495 and the effect of supplying it to
496 .Fn regexec
497 or
498 .Fn regerror
499 is undefined.
500 .Pp
501 None of these functions references global variables except for tables
502 of constants;
503 all are safe for use from multiple threads if the arguments are safe.
504 .Sh IMPLEMENTATION CHOICES
505 There are a number of decisions that
506 .St -p1003.2
507 leaves up to the implementor,
508 either by explicitly saying
509 .Dq undefined
510 or by virtue of them being
511 forbidden by the RE grammar.
512 This implementation treats them as follows.
513 .Pp
514 See
515 .Xr re_format 7
516 for a discussion of the definition of case-independent matching.
517 .Pp
518 There is no particular limit on the length of REs,
519 except insofar as memory is limited.
520 Memory usage is approximately linear in RE size, and largely insensitive
521 to RE complexity, except for bounded repetitions.
522 See
523 .Sx BUGS
524 for one short RE using them
525 that will run almost any system out of memory.
526 .Pp
527 A backslashed character other than one specifically given a magic meaning
528 by
529 .St -p1003.2
530 (such magic meanings occur only in obsolete
531 .Bq Dq basic
532 REs)
533 is taken as an ordinary character.
534 .Pp
535 Any unmatched
536 .Ql [\&
537 is a
538 .Dv REG_EBRACK
539 error.
540 .Pp
541 Equivalence classes cannot begin or end bracket-expression ranges.
542 The endpoint of one range cannot begin another.
543 .Pp
544 .Dv RE_DUP_MAX ,
545 the limit on repetition counts in bounded repetitions, is 255.
546 .Pp
547 A repetition operator
548 .Ql ( ?\& ,
549 .Ql *\& ,
550 .Ql +\& ,
551 or bounds)
552 cannot follow another
553 repetition operator.
554 A repetition operator cannot begin an expression or subexpression
555 or follow
556 .Ql ^\&
557 or
558 .Ql |\& .
559 .Pp
560 .Ql |\&
561 cannot appear first or last in a (sub)expression or after another
562 .Ql |\& ,
563 i.e., an operand of
564 .Ql |\&
565 cannot be an empty subexpression.
566 An empty parenthesized subexpression,
567 .Ql "()" ,
568 is legal and matches an
569 empty (sub)string.
570 An empty string is not a legal RE.
571 .Pp
572 A
573 .Ql {\&
574 followed by a digit is considered the beginning of bounds for a
575 bounded repetition, which must then follow the syntax for bounds.
576 A
577 .Ql {\&
578 .Em not
579 followed by a digit is considered an ordinary character.
580 .Pp
581 .Ql ^\&
582 and
583 .Ql $\&
584 beginning and ending subexpressions in obsolete
585 .Pq Dq basic
586 REs are anchors, not ordinary characters.
587 .Sh DIAGNOSTICS
588 Non-zero error codes from
589 .Fn regcomp
590 and
591 .Fn regexec
592 include the following:
593 .Pp
594 .Bl -tag -width REG_ECOLLATE -compact
595 .It Dv REG_NOMATCH
596 The
597 .Fn regexec
598 function
599 failed to match
600 .It Dv REG_BADPAT
601 invalid regular expression
602 .It Dv REG_ECOLLATE
603 invalid collating element
604 .It Dv REG_ECTYPE
605 invalid character class
606 .It Dv REG_EESCAPE
607 .Ql \e
608 applied to unescapable character
609 .It Dv REG_ESUBREG
610 invalid backreference number
611 .It Dv REG_EBRACK
612 brackets
613 .Ql "[ ]"
614 not balanced
615 .It Dv REG_EPAREN
616 parentheses
617 .Ql "( )"
618 not balanced
619 .It Dv REG_EBRACE
620 braces
621 .Ql "{ }"
622 not balanced
623 .It Dv REG_BADBR
624 invalid repetition count(s) in
625 .Ql "{ }"
626 .It Dv REG_ERANGE
627 invalid character range in
628 .Ql "[ ]"
629 .It Dv REG_ESPACE
630 ran out of memory
631 .It Dv REG_BADRPT
632 .Ql ?\& ,
633 .Ql *\& ,
634 or
635 .Ql +\&
636 operand invalid
637 .It Dv REG_EMPTY
638 empty (sub)expression
639 .It Dv REG_ASSERT
640 cannot happen - you found a bug
641 .It Dv REG_INVARG
642 invalid argument, e.g.\& negative-length string
643 .It Dv REG_ILLSEQ
644 illegal byte sequence (bad multibyte character)
645 .El
646 .Sh SEE ALSO
647 .Xr grep 1 ,
648 .Xr re_format 7
649 .Pp
650 .St -p1003.2 ,
651 sections 2.8 (Regular Expression Notation)
652 and
653 B.5 (C Binding for Regular Expression Matching).
654 .Sh HISTORY
655 Originally written by
656 .An Henry Spencer .
657 Altered for inclusion in the
658 .Bx 4.4
659 distribution.
660 .Sh BUGS
661 This is an alpha release with known defects.
662 Please report problems.
663 .Pp
664 The back-reference code is subtle and doubts linger about its correctness
665 in complex cases.
666 .Pp
667 The
668 .Fn regexec
669 function
670 performance is poor.
671 This will improve with later releases.
672 The
673 .Fa nmatch
674 argument
675 exceeding 0 is expensive;
676 .Fa nmatch
677 exceeding 1 is worse.
678 The
679 .Fn regexec
680 function
681 is largely insensitive to RE complexity
682 .Em except
683 that back
684 references are massively expensive.
685 RE length does matter; in particular, there is a strong speed bonus
686 for keeping RE length under about 30 characters,
687 with most special characters counting roughly double.
688 .Pp
689 The
690 .Fn regcomp
691 function
692 implements bounded repetitions by macro expansion,
693 which is costly in time and space if counts are large
694 or bounded repetitions are nested.
695 An RE like, say,
696 .Ql "((((a{1,100}){1,100}){1,100}){1,100}){1,100}"
697 will (eventually) run almost any existing machine out of swap space.
698 .Pp
699 There are suspected problems with response to obscure error conditions.
700 Notably,
701 certain kinds of internal overflow,
702 produced only by truly enormous REs or by multiply nested bounded repetitions,
703 are probably not handled well.
704 .Pp
705 Due to a mistake in
706 .St -p1003.2 ,
707 things like
708 .Ql "a)b"
709 are legal REs because
710 .Ql )\&
711 is
712 a special character only in the presence of a previous unmatched
713 .Ql (\& .
714 This cannot be fixed until the spec is fixed.
715 .Pp
716 The standard's definition of back references is vague.
717 For example, does
718 .Ql "a\e(\e(b\e)*\e2\e)*d"
719 match
720 .Ql "abbbd" ?
721 Until the standard is clarified,
722 behavior in such cases should not be relied on.
723 .Pp
724 The implementation of word-boundary matching is a bit of a kludge,
725 and bugs may lurk in combinations of word-boundary matching and anchoring.
726 .Pp
727 Word-boundary matching does not work properly in multibyte locales.