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.
5 .\" This code is derived from software contributed to Berkeley by
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)regex.3 8.4 (Berkeley) 3/20/94
37 .\" $FreeBSD: src/lib/libc/regex/regex.3,v 1.4.2.4 2001/12/14 18:33:56 ru Exp $
38 .\" $DragonFly: src/lib/libc/regex/regex.3,v 1.2 2003/06/17 04:26:44 dillon Exp $
48 .Nd regular-expression library
55 .Fn regcomp "regex_t *preg" "const char *pattern" "int cflags"
58 .Fa "const regex_t *preg" "const char *string"
59 .Fa "size_t nmatch" "regmatch_t pmatch[]" "int eflags"
63 .Fa "int errcode" "const regex_t *preg"
64 .Fa "char *errbuf" "size_t errbuf_size"
67 .Fn regfree "regex_t *preg"
69 These routines implement
76 compiles an RE written as a string into an internal form,
78 matches that internal form against a string and reports results,
80 transforms error codes from either into human-readable messages,
83 frees any dynamically-allocated storage used by the internal form
88 declares two structure types,
92 the former for compiled internal forms and the latter for match reporting.
93 It also declares the four functions,
96 and a number of constants with names starting with
100 compiles the regular expression contained in the
103 subject to the flags in
105 and places the results in the
107 structure pointed to by
110 is the bitwise OR of zero or more of the following flags:
111 .Bl -tag -width REG_EXTENDED
116 rather than the obsolete
121 This is a synonym for 0,
122 provided as a counterpart to
124 to improve readability.
126 Compile with recognition of all special characters turned off.
127 All characters are thus considered ordinary,
131 This is an extension,
132 compatible with but not specified by
134 and should be used with
135 caution in software intended to be portable to other systems.
143 Compile for matching that ignores upper/lower case distinctions.
147 Compile for matching that need only report success or failure,
148 not what was matched.
150 Compile for newline-sensitive matching.
151 By default, newline is a completely ordinary character with no special
152 meaning in either REs or strings.
155 bracket expressions and
160 anchor matches the null string after any newline in the string
161 in addition to its normal function,
164 anchor matches the null string before any newline in the
165 string in addition to its normal function.
167 The regular expression ends,
168 not at the first NUL,
169 but just before the character pointed to by the
171 member of the structure pointed to by
177 This flag permits inclusion of NULs in the RE;
178 they are considered ordinary characters.
179 This is an extension,
180 compatible with but not specified by
182 and should be used with
183 caution in software intended to be portable to other systems.
188 returns 0 and fills in the structure pointed to by
190 One member of that structure
197 contains the number of parenthesized subexpressions within the RE
198 (except that the value of this member is undefined if the
203 fails, it returns a non-zero error code;
208 matches the compiled RE pointed to by
212 subject to the flags in
214 and reports results using
217 and the returned value.
218 The RE must have been compiled by a previous invocation of
220 The compiled form is not altered during execution of
222 so a single compiled RE can be used simultaneously by multiple threads.
225 the NUL-terminated string pointed to by
227 is considered to be the text of an entire line, minus any terminating
231 argument is the bitwise OR of zero or more of the following flags:
232 .Bl -tag -width REG_STARTEND
234 The first character of
236 is not the beginning of a line, so the
238 anchor should not match before it.
239 This does not affect the behavior of newlines under
244 does not end a line, so the
246 anchor should not match before it.
247 This does not affect the behavior of newlines under
250 The string is considered to start at
253 .Fa pmatch Ns [0]. Ns Va rm_so
254 and to have a terminating NUL located at
257 .Fa pmatch Ns [0]. Ns Va rm_eo
258 (there need not actually be a NUL at that location),
259 regardless of the value of
261 See below for the definition of
265 This is an extension,
266 compatible with but not specified by
268 and should be used with
269 caution in software intended to be portable to other systems.
275 affects only the location of the string,
276 not how it is matched.
281 for a discussion of what is matched in situations where an RE or a
282 portion thereof could match any of several substrings of
287 returns 0 for success and the non-zero code
290 Other non-zero error codes may be returned in exceptional situations;
296 was specified in the compilation of the RE,
303 argument (but see below for the case where
308 points to an array of
312 Such a structure has at least the members
318 (a signed arithmetic type at least as large as an
322 containing respectively the offset of the first character of a substring
323 and the offset of the first character after the end of the substring.
324 Offsets are measured from the beginning of the
328 An empty substring is denoted by equal offsets,
329 both indicating the character following the empty substring.
331 The 0th member of the
333 array is filled in to indicate what substring of
335 was matched by the entire RE.
336 Remaining members report what substring was matched by parenthesized
337 subexpressions within the RE;
340 reports subexpression
342 with subexpressions counted (starting at 1) by the order of their opening
343 parentheses in the RE, left to right.
344 Unused entries in the array (corresponding either to subexpressions that
345 did not participate in the match at all, or to subexpressions that do not
346 exist in the RE (that is,
349 .Fa preg Ns -> Ns Va re_nsub ) )
355 If a subexpression participated in the match several times,
356 the reported substring is the last one it matched.
357 (Note, as an example in particular, that when the RE
361 the parenthesized subexpression matches each of the three
364 an infinite number of empty strings following the last
366 so the reported substring is one of the empties.)
372 must point to at least one
379 to hold the input offsets for
381 Use for output is still entirely controlled by
390 will not be changed by a successful
400 to a human-readable, printable message.
404 .No non\- Ns Dv NULL ,
405 the error code should have arisen from use of
410 and if the error code came from
412 it should have been the result from the most recent
417 may be able to supply a more detailed message using information
421 places the NUL-terminated message into the buffer pointed to by
423 limiting the length (including the NUL) to at most
426 If the whole message won't fit,
427 as much of it as will fit before the terminating NUL is supplied.
429 the returned value is the size of buffer needed to hold the whole
430 message (including terminating NUL).
435 is ignored but the return value is still correct.
445 that results is the printable name of the error code,
448 rather than an explanation thereof.
459 member of the structure it points to
460 must point to the printable name of an error code;
461 in this case, the result in
463 is the decimal digits of
464 the numeric value of the error code
465 (0 if the name is not recognized).
469 are intended primarily as debugging facilities;
471 compatible with but not specified by
473 and should be used with
474 caution in software intended to be portable to other systems.
475 Be warned also that they are considered experimental and changes are possible.
478 frees any dynamically-allocated storage associated with the compiled RE
483 is no longer a valid compiled RE
484 and the effect of supplying it to
490 None of these functions references global variables except for tables
492 all are safe for use from multiple threads if the arguments are safe.
493 .Sh IMPLEMENTATION CHOICES
494 There are a number of decisions that
496 leaves up to the implementor,
497 either by explicitly saying
499 or by virtue of them being
500 forbidden by the RE grammar.
501 This implementation treats them as follows.
505 for a discussion of the definition of case-independent matching.
507 There is no particular limit on the length of REs,
508 except insofar as memory is limited.
509 Memory usage is approximately linear in RE size, and largely insensitive
510 to RE complexity, except for bounded repetitions.
513 for one short RE using them
514 that will run almost any system out of memory.
516 A backslashed character other than one specifically given a magic meaning
519 (such magic meanings occur only in obsolete
522 is taken as an ordinary character.
530 Equivalence classes cannot begin or end bracket-expression ranges.
531 The endpoint of one range cannot begin another.
534 the limit on repetition counts in bounded repetitions, is 255.
536 A repetition operator
541 cannot follow another
543 A repetition operator cannot begin an expression or subexpression
550 cannot appear first or last in a (sub)expression or after another
554 cannot be an empty subexpression.
555 An empty parenthesized subexpression,
557 is legal and matches an
559 An empty string is not a legal RE.
563 followed by a digit is considered the beginning of bounds for a
564 bounded repetition, which must then follow the syntax for bounds.
568 followed by a digit is considered an ordinary character.
573 beginning and ending subexpressions in obsolete
575 REs are anchors, not ordinary characters.
581 sections 2.8 (Regular Expression Notation)
583 B.5 (C Binding for Regular Expression Matching).
585 Non-zero error codes from
589 include the following:
591 .Bl -tag -width REG_ECOLLATE -compact
596 invalid regular expression
598 invalid collating element
600 invalid character class
603 applied to unescapable character
605 invalid backreference number
619 invalid repetition count(s) in
622 invalid character range in
633 empty (sub)expression
635 can't happen - you found a bug
637 invalid argument, e.g. negative-length string
640 Originally written by
642 Altered for inclusion in the
646 This is an alpha release with known defects.
647 Please report problems.
649 The back-reference code is subtle and doubts linger about its correctness
654 This will improve with later releases.
656 exceeding 0 is expensive;
658 exceeding 1 is worse.
660 is largely insensitive to RE complexity
663 references are massively expensive.
664 RE length does matter; in particular, there is a strong speed bonus
665 for keeping RE length under about 30 characters,
666 with most special characters counting roughly double.
669 implements bounded repetitions by macro expansion,
670 which is costly in time and space if counts are large
671 or bounded repetitions are nested.
673 .Ql "((((a{1,100}){1,100}){1,100}){1,100}){1,100}"
674 will (eventually) run almost any existing machine out of swap space.
676 There are suspected problems with response to obscure error conditions.
678 certain kinds of internal overflow,
679 produced only by truly enormous REs or by multiply nested bounded repetitions,
680 are probably not handled well.
686 are legal REs because
689 a special character only in the presence of a previous unmatched
691 This can't be fixed until the spec is fixed.
693 The standard's definition of back references is vague.
695 .Ql "a\e(\e(b\e)*\e2\e)*d"
698 Until the standard is clarified,
699 behavior in such cases should not be relied on.
701 The implementation of word-boundary matching is a bit of a kludge,
702 and bugs may lurk in combinations of word-boundary matching and anchoring.