freebsd-dev/lib/libc/regex/regex.3

756 lines
18 KiB
Groff
Raw Normal View History

1994-05-27 05:00:24 +00:00
.\" Copyright (c) 1992, 1993, 1994 Henry Spencer.
.\" Copyright (c) 1992, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Henry Spencer.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
1994-05-27 05:00:24 +00:00
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)regex.3 8.4 (Berkeley) 3/20/94
1999-08-28 00:22:10 +00:00
.\" $FreeBSD$
1994-05-27 05:00:24 +00:00
.\"
.Dd May 25, 2016
2001-01-12 13:35:17 +00:00
.Dt REGEX 3
.Os
.Sh NAME
.Nm regcomp ,
.Nm regexec ,
.Nm regerror ,
.Nm regfree
.Nd regular-expression library
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In regex.h
2001-01-12 13:35:17 +00:00
.Ft int
2002-10-02 07:49:35 +00:00
.Fo regcomp
.Fa "regex_t * restrict preg" "const char * restrict pattern" "int cflags"
.Fc
2001-01-12 13:35:17 +00:00
.Ft int
.Fo regexec
2002-10-02 07:49:35 +00:00
.Fa "const regex_t * restrict preg" "const char * restrict string"
.Fa "size_t nmatch" "regmatch_t pmatch[restrict]" "int eflags"
2001-01-12 13:35:17 +00:00
.Fc
.Ft size_t
.Fo regerror
2002-10-02 07:49:35 +00:00
.Fa "int errcode" "const regex_t * restrict preg"
.Fa "char * restrict errbuf" "size_t errbuf_size"
2001-01-12 13:35:17 +00:00
.Fc
.Ft void
.Fn regfree "regex_t *preg"
.Sh DESCRIPTION
These routines implement
.St -p1003.2
regular expressions
.Pq Do RE Dc Ns s ;
1994-05-27 05:00:24 +00:00
see
2001-01-12 13:35:17 +00:00
.Xr re_format 7 .
2002-12-18 12:45:11 +00:00
The
.Fn regcomp
function
1994-05-27 05:00:24 +00:00
compiles an RE written as a string into an internal form,
2001-01-12 13:35:17 +00:00
.Fn regexec
1994-05-27 05:00:24 +00:00
matches that internal form against a string and reports results,
2001-01-12 13:35:17 +00:00
.Fn regerror
1994-05-27 05:00:24 +00:00
transforms error codes from either into human-readable messages,
and
2001-01-12 13:35:17 +00:00
.Fn regfree
1994-05-27 05:00:24 +00:00
frees any dynamically-allocated storage used by the internal form
of an RE.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
The header
.In regex.h
1994-05-27 05:00:24 +00:00
declares two structure types,
2001-01-12 13:35:17 +00:00
.Ft regex_t
1994-05-27 05:00:24 +00:00
and
2001-01-12 13:35:17 +00:00
.Ft regmatch_t ,
1994-05-27 05:00:24 +00:00
the former for compiled internal forms and the latter for match reporting.
It also declares the four functions,
a type
2001-01-12 13:35:17 +00:00
.Ft regoff_t ,
and a number of constants with names starting with
.Dq Dv REG_ .
.Pp
2002-12-18 12:45:11 +00:00
The
.Fn regcomp
function
1994-05-27 05:00:24 +00:00
compiles the regular expression contained in the
2001-01-12 13:35:17 +00:00
.Fa pattern
1994-05-27 05:00:24 +00:00
string,
subject to the flags in
2001-01-12 13:35:17 +00:00
.Fa cflags ,
1994-05-27 05:00:24 +00:00
and places the results in the
2001-01-12 13:35:17 +00:00
.Ft regex_t
1994-05-27 05:00:24 +00:00
structure pointed to by
2001-01-12 13:35:17 +00:00
.Fa preg .
2002-12-19 09:40:28 +00:00
The
.Fa cflags
argument
1994-05-27 05:00:24 +00:00
is the bitwise OR of zero or more of the following flags:
2001-01-12 13:35:17 +00:00
.Bl -tag -width REG_EXTENDED
.It Dv REG_EXTENDED
Compile modern
.Pq Dq extended
REs,
rather than the obsolete
.Pq Dq basic
REs that
1994-05-27 05:00:24 +00:00
are the default.
2001-01-12 13:35:17 +00:00
.It Dv REG_BASIC
1994-05-27 05:00:24 +00:00
This is a synonym for 0,
2001-01-12 13:35:17 +00:00
provided as a counterpart to
.Dv REG_EXTENDED
to improve readability.
.It Dv REG_NOSPEC
1994-05-27 05:00:24 +00:00
Compile with recognition of all special characters turned off.
All characters are thus considered ordinary,
2001-01-12 13:35:17 +00:00
so the
.Dq RE
is a literal string.
1994-05-27 05:00:24 +00:00
This is an extension,
2001-01-12 13:35:17 +00:00
compatible with but not specified by
.St -p1003.2 ,
1994-05-27 05:00:24 +00:00
and should be used with
caution in software intended to be portable to other systems.
2001-01-12 13:35:17 +00:00
.Dv REG_EXTENDED
and
.Dv REG_NOSPEC
may not be used
1994-05-27 05:00:24 +00:00
in the same call to
2001-01-12 13:35:17 +00:00
.Fn regcomp .
.It Dv REG_ICASE
1994-05-27 05:00:24 +00:00
Compile for matching that ignores upper/lower case distinctions.
See
2001-01-12 13:35:17 +00:00
.Xr re_format 7 .
.It Dv REG_NOSUB
1994-05-27 05:00:24 +00:00
Compile for matching that need only report success or failure,
not what was matched.
2001-01-12 13:35:17 +00:00
.It Dv REG_NEWLINE
1994-05-27 05:00:24 +00:00
Compile for newline-sensitive matching.
By default, newline is a completely ordinary character with no special
meaning in either REs or strings.
With this flag,
2001-01-12 13:35:17 +00:00
.Ql [^
bracket expressions and
.Ql .\&
never match newline,
a
.Ql ^\&
anchor matches the null string after any newline in the string
1994-05-27 05:00:24 +00:00
in addition to its normal function,
2001-01-12 13:35:17 +00:00
and the
.Ql $\&
anchor matches the null string before any newline in the
1994-05-27 05:00:24 +00:00
string in addition to its normal function.
2001-01-12 13:35:17 +00:00
.It Dv REG_PEND
1994-05-27 05:00:24 +00:00
The regular expression ends,
not at the first NUL,
but just before the character pointed to by the
2001-01-12 13:35:17 +00:00
.Va re_endp
1994-05-27 05:00:24 +00:00
member of the structure pointed to by
2001-01-12 13:35:17 +00:00
.Fa preg .
1994-05-27 05:00:24 +00:00
The
2001-01-12 13:35:17 +00:00
.Va re_endp
1994-05-27 05:00:24 +00:00
member is of type
2001-01-12 13:35:17 +00:00
.Ft "const char *" .
1994-05-27 05:00:24 +00:00
This flag permits inclusion of NULs in the RE;
they are considered ordinary characters.
This is an extension,
2001-01-12 13:35:17 +00:00
compatible with but not specified by
.St -p1003.2 ,
1994-05-27 05:00:24 +00:00
and should be used with
caution in software intended to be portable to other systems.
2001-01-12 13:35:17 +00:00
.El
.Pp
1994-05-27 05:00:24 +00:00
When successful,
2001-01-12 13:35:17 +00:00
.Fn regcomp
1994-05-27 05:00:24 +00:00
returns 0 and fills in the structure pointed to by
2001-01-12 13:35:17 +00:00
.Fa preg .
1994-05-27 05:00:24 +00:00
One member of that structure
(other than
2001-01-12 13:35:17 +00:00
.Va re_endp )
1994-05-27 05:00:24 +00:00
is publicized:
2001-01-12 13:35:17 +00:00
.Va re_nsub ,
1994-05-27 05:00:24 +00:00
of type
2001-01-12 13:35:17 +00:00
.Ft size_t ,
1994-05-27 05:00:24 +00:00
contains the number of parenthesized subexpressions within the RE
(except that the value of this member is undefined if the
2001-01-12 13:35:17 +00:00
.Dv REG_NOSUB
flag was used).
1994-05-27 05:00:24 +00:00
If
2001-01-12 13:35:17 +00:00
.Fn regcomp
1994-05-27 05:00:24 +00:00
fails, it returns a non-zero error code;
2001-01-12 13:35:17 +00:00
see
.Sx DIAGNOSTICS .
.Pp
2002-12-18 12:45:11 +00:00
The
.Fn regexec
function
1994-05-27 05:00:24 +00:00
matches the compiled RE pointed to by
2001-01-12 13:35:17 +00:00
.Fa preg
1994-05-27 05:00:24 +00:00
against the
2001-01-12 13:35:17 +00:00
.Fa string ,
1994-05-27 05:00:24 +00:00
subject to the flags in
2001-01-12 13:35:17 +00:00
.Fa eflags ,
1994-05-27 05:00:24 +00:00
and reports results using
2001-01-12 13:35:17 +00:00
.Fa nmatch ,
.Fa pmatch ,
1994-05-27 05:00:24 +00:00
and the returned value.
The RE must have been compiled by a previous invocation of
2001-01-12 13:35:17 +00:00
.Fn regcomp .
1994-05-27 05:00:24 +00:00
The compiled form is not altered during execution of
2001-01-12 13:35:17 +00:00
.Fn regexec ,
1994-05-27 05:00:24 +00:00
so a single compiled RE can be used simultaneously by multiple threads.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
By default,
the NUL-terminated string pointed to by
2001-01-12 13:35:17 +00:00
.Fa string
1994-05-27 05:00:24 +00:00
is considered to be the text of an entire line, minus any terminating
newline.
The
2001-01-12 13:35:17 +00:00
.Fa eflags
1994-05-27 05:00:24 +00:00
argument is the bitwise OR of zero or more of the following flags:
2001-01-12 13:35:17 +00:00
.Bl -tag -width REG_STARTEND
.It Dv REG_NOTBOL
The first character of the string is treated as the continuation
of a line.
This means that the anchors
.Ql ^\& ,
.Ql [[:<:]] ,
and
.Ql \e<
do not match before it; but see
.Dv REG_STARTEND
below.
2001-01-12 13:35:17 +00:00
This does not affect the behavior of newlines under
.Dv REG_NEWLINE .
.It Dv REG_NOTEOL
1994-05-27 05:00:24 +00:00
The NUL terminating
the string
2001-01-12 13:35:17 +00:00
does not end a line, so the
.Ql $\&
anchor does not match before it.
2001-01-12 13:35:17 +00:00
This does not affect the behavior of newlines under
.Dv REG_NEWLINE .
.It Dv REG_STARTEND
1994-05-27 05:00:24 +00:00
The string is considered to start at
.Fa string No +
.Fa pmatch Ns [0]. Ns Fa rm_so
and to end before the byte located at
.Fa string No +
.Fa pmatch Ns [0]. Ns Fa rm_eo ,
1994-05-27 05:00:24 +00:00
regardless of the value of
2001-01-12 13:35:17 +00:00
.Fa nmatch .
1994-05-27 05:00:24 +00:00
See below for the definition of
2001-01-12 13:35:17 +00:00
.Fa pmatch
1994-05-27 05:00:24 +00:00
and
2001-01-12 13:35:17 +00:00
.Fa nmatch .
1994-05-27 05:00:24 +00:00
This is an extension,
2001-01-12 13:35:17 +00:00
compatible with but not specified by
.St -p1003.2 ,
1994-05-27 05:00:24 +00:00
and should be used with
caution in software intended to be portable to other systems.
.Pp
Without
.Dv REG_NOTBOL ,
the position
.Fa rm_so
is considered the beginning of a line, such that
.Ql ^
matches before it, and the beginning of a word if there is a word
character at this position, such that
.Ql [[:<:]]
and
.Ql \e<
match before it.
.Pp
With
.Dv REG_NOTBOL ,
the character at position
.Fa rm_so
is treated as the continuation of a line, and if
.Fa rm_so
is greater than 0, the preceding character is taken into consideration.
If the preceding character is a newline and the regular expression was compiled
with
.Dv REG_NEWLINE ,
.Ql ^
matches before the string; if the preceding character is not a word character
but the string starts with a word character,
.Ql [[:<:]]
and
.Ql \e<
match before the string.
2001-01-12 13:35:17 +00:00
.El
.Pp
1994-05-27 05:00:24 +00:00
See
2001-01-12 13:35:17 +00:00
.Xr re_format 7
1994-05-27 05:00:24 +00:00
for a discussion of what is matched in situations where an RE or a
portion thereof could match any of several substrings of
2001-01-12 13:35:17 +00:00
.Fa string .
.Pp
1994-05-27 05:00:24 +00:00
Normally,
2001-01-12 13:35:17 +00:00
.Fn regexec
returns 0 for success and the non-zero code
.Dv REG_NOMATCH
for failure.
1994-05-27 05:00:24 +00:00
Other non-zero error codes may be returned in exceptional situations;
2001-01-12 13:35:17 +00:00
see
.Sx DIAGNOSTICS .
.Pp
If
.Dv REG_NOSUB
was specified in the compilation of the RE,
1994-05-27 05:00:24 +00:00
or if
2001-01-12 13:35:17 +00:00
.Fa nmatch
1994-05-27 05:00:24 +00:00
is 0,
2001-01-12 13:35:17 +00:00
.Fn regexec
1994-05-27 05:00:24 +00:00
ignores the
2001-01-12 13:35:17 +00:00
.Fa pmatch
argument (but see below for the case where
.Dv REG_STARTEND
is specified).
1994-05-27 05:00:24 +00:00
Otherwise,
2001-01-12 13:35:17 +00:00
.Fa pmatch
1994-05-27 05:00:24 +00:00
points to an array of
2001-01-12 13:35:17 +00:00
.Fa nmatch
1994-05-27 05:00:24 +00:00
structures of type
2001-01-12 13:35:17 +00:00
.Ft regmatch_t .
1994-05-27 05:00:24 +00:00
Such a structure has at least the members
2001-01-12 13:35:17 +00:00
.Va rm_so
1994-05-27 05:00:24 +00:00
and
2001-01-12 13:35:17 +00:00
.Va rm_eo ,
1994-05-27 05:00:24 +00:00
both of type
2001-01-12 13:35:17 +00:00
.Ft regoff_t
1994-05-27 05:00:24 +00:00
(a signed arithmetic type at least as large as an
2001-01-12 13:35:17 +00:00
.Ft off_t
1994-05-27 05:00:24 +00:00
and a
2001-01-12 13:35:17 +00:00
.Ft ssize_t ) ,
1994-05-27 05:00:24 +00:00
containing respectively the offset of the first character of a substring
and the offset of the first character after the end of the substring.
Offsets are measured from the beginning of the
2001-01-12 13:35:17 +00:00
.Fa string
1994-05-27 05:00:24 +00:00
argument given to
2001-01-12 13:35:17 +00:00
.Fn regexec .
1994-05-27 05:00:24 +00:00
An empty substring is denoted by equal offsets,
both indicating the character following the empty substring.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
The 0th member of the
2001-01-12 13:35:17 +00:00
.Fa pmatch
1994-05-27 05:00:24 +00:00
array is filled in to indicate what substring of
2001-01-12 13:35:17 +00:00
.Fa string
1994-05-27 05:00:24 +00:00
was matched by the entire RE.
Remaining members report what substring was matched by parenthesized
subexpressions within the RE;
member
2001-01-12 13:35:17 +00:00
.Va i
1994-05-27 05:00:24 +00:00
reports subexpression
2001-01-12 13:35:17 +00:00
.Va i ,
1994-05-27 05:00:24 +00:00
with subexpressions counted (starting at 1) by the order of their opening
parentheses in the RE, left to right.
2001-01-12 13:35:17 +00:00
Unused entries in the array (corresponding either to subexpressions that
1994-05-27 05:00:24 +00:00
did not participate in the match at all, or to subexpressions that do not
2001-01-12 13:35:17 +00:00
exist in the RE (that is,
.Va i
>
.Fa preg Ns -> Ns Va re_nsub ) )
have both
.Va rm_so
1994-05-27 05:00:24 +00:00
and
2001-01-12 13:35:17 +00:00
.Va rm_eo
set to -1.
1994-05-27 05:00:24 +00:00
If a subexpression participated in the match several times,
the reported substring is the last one it matched.
2001-01-12 13:35:17 +00:00
(Note, as an example in particular, that when the RE
.Ql "(b*)+"
matches
.Ql bbb ,
the parenthesized subexpression matches each of the three
.So Li b Sc Ns s
and then
an infinite number of empty strings following the last
.Ql b ,
1994-05-27 05:00:24 +00:00
so the reported substring is one of the empties.)
2001-01-12 13:35:17 +00:00
.Pp
If
.Dv REG_STARTEND
is specified,
.Fa pmatch
1994-05-27 05:00:24 +00:00
must point to at least one
2001-01-12 13:35:17 +00:00
.Ft regmatch_t
1994-05-27 05:00:24 +00:00
(even if
2001-01-12 13:35:17 +00:00
.Fa nmatch
is 0 or
.Dv REG_NOSUB
was specified),
to hold the input offsets for
.Dv REG_STARTEND .
1994-05-27 05:00:24 +00:00
Use for output is still entirely controlled by
2001-01-12 13:35:17 +00:00
.Fa nmatch ;
1994-05-27 05:00:24 +00:00
if
2001-01-12 13:35:17 +00:00
.Fa nmatch
is 0 or
.Dv REG_NOSUB
was specified,
1994-05-27 05:00:24 +00:00
the value of
2001-01-12 13:35:17 +00:00
.Fa pmatch Ns [0]
1994-05-27 05:00:24 +00:00
will not be changed by a successful
2001-01-12 13:35:17 +00:00
.Fn regexec .
.Pp
2002-12-18 12:45:11 +00:00
The
.Fn regerror
function
1994-05-27 05:00:24 +00:00
maps a non-zero
2001-01-12 13:35:17 +00:00
.Fa errcode
1994-05-27 05:00:24 +00:00
from either
2001-01-12 13:35:17 +00:00
.Fn regcomp
1994-05-27 05:00:24 +00:00
or
2001-01-12 13:35:17 +00:00
.Fn regexec
1994-05-27 05:00:24 +00:00
to a human-readable, printable message.
If
2001-01-12 13:35:17 +00:00
.Fa preg
is
.No non\- Ns Dv NULL ,
1994-05-27 05:00:24 +00:00
the error code should have arisen from use of
the
2001-01-12 13:35:17 +00:00
.Ft regex_t
1994-05-27 05:00:24 +00:00
pointed to by
2001-01-12 13:35:17 +00:00
.Fa preg ,
1994-05-27 05:00:24 +00:00
and if the error code came from
2001-01-12 13:35:17 +00:00
.Fn regcomp ,
1994-05-27 05:00:24 +00:00
it should have been the result from the most recent
2001-01-12 13:35:17 +00:00
.Fn regcomp
1994-05-27 05:00:24 +00:00
using that
2001-01-12 13:35:17 +00:00
.Ft regex_t .
2002-12-18 12:45:11 +00:00
The
2015-04-26 10:55:39 +00:00
.Po
.Fn regerror
1994-05-27 05:00:24 +00:00
may be able to supply a more detailed message using information
from the
2015-04-26 10:55:39 +00:00
.Ft regex_t .
.Pc
2002-12-18 12:45:11 +00:00
The
.Fn regerror
function
1994-05-27 05:00:24 +00:00
places the NUL-terminated message into the buffer pointed to by
2001-01-12 13:35:17 +00:00
.Fa errbuf ,
1994-05-27 05:00:24 +00:00
limiting the length (including the NUL) to at most
2001-01-12 13:35:17 +00:00
.Fa errbuf_size
1994-05-27 05:00:24 +00:00
bytes.
2005-02-13 22:25:33 +00:00
If the whole message will not fit,
1994-05-27 05:00:24 +00:00
as much of it as will fit before the terminating NUL is supplied.
In any case,
the returned value is the size of buffer needed to hold the whole
message (including terminating NUL).
If
2001-01-12 13:35:17 +00:00
.Fa errbuf_size
1994-05-27 05:00:24 +00:00
is 0,
2001-01-12 13:35:17 +00:00
.Fa errbuf
1994-05-27 05:00:24 +00:00
is ignored but the return value is still correct.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
If the
2001-01-12 13:35:17 +00:00
.Fa errcode
1994-05-27 05:00:24 +00:00
given to
2001-01-12 13:35:17 +00:00
.Fn regerror
is first ORed with
.Dv REG_ITOA ,
the
.Dq message
that results is the printable name of the error code,
e.g.\&
2001-01-12 13:35:17 +00:00
.Dq Dv REG_NOMATCH ,
1994-05-27 05:00:24 +00:00
rather than an explanation thereof.
If
2001-01-12 13:35:17 +00:00
.Fa errcode
is
.Dv REG_ATOI ,
1994-05-27 05:00:24 +00:00
then
2001-01-12 13:35:17 +00:00
.Fa preg
shall be
.No non\- Ns Dv NULL
and the
.Va re_endp
1994-05-27 05:00:24 +00:00
member of the structure it points to
must point to the printable name of an error code;
in this case, the result in
2001-01-12 13:35:17 +00:00
.Fa errbuf
1994-05-27 05:00:24 +00:00
is the decimal digits of
the numeric value of the error code
(0 if the name is not recognized).
2001-01-12 13:35:17 +00:00
.Dv REG_ITOA
and
.Dv REG_ATOI
are intended primarily as debugging facilities;
1994-05-27 05:00:24 +00:00
they are extensions,
2001-01-12 13:35:17 +00:00
compatible with but not specified by
.St -p1003.2 ,
1994-05-27 05:00:24 +00:00
and should be used with
caution in software intended to be portable to other systems.
Be warned also that they are considered experimental and changes are possible.
2001-01-12 13:35:17 +00:00
.Pp
2002-12-18 12:45:11 +00:00
The
.Fn regfree
function
1994-05-27 05:00:24 +00:00
frees any dynamically-allocated storage associated with the compiled RE
pointed to by
2001-01-12 13:35:17 +00:00
.Fa preg .
1994-05-27 05:00:24 +00:00
The remaining
2001-01-12 13:35:17 +00:00
.Ft regex_t
1994-05-27 05:00:24 +00:00
is no longer a valid compiled RE
and the effect of supplying it to
2001-01-12 13:35:17 +00:00
.Fn regexec
1994-05-27 05:00:24 +00:00
or
2001-01-12 13:35:17 +00:00
.Fn regerror
1994-05-27 05:00:24 +00:00
is undefined.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
None of these functions references global variables except for tables
of constants;
all are safe for use from multiple threads if the arguments are safe.
2001-01-12 13:35:17 +00:00
.Sh IMPLEMENTATION CHOICES
There are a number of decisions that
.St -p1003.2
leaves up to the implementor,
either by explicitly saying
.Dq undefined
or by virtue of them being
1994-05-27 05:00:24 +00:00
forbidden by the RE grammar.
This implementation treats them as follows.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
See
2001-01-12 13:35:17 +00:00
.Xr re_format 7
1994-05-27 05:00:24 +00:00
for a discussion of the definition of case-independent matching.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
There is no particular limit on the length of REs,
except insofar as memory is limited.
Memory usage is approximately linear in RE size, and largely insensitive
to RE complexity, except for bounded repetitions.
2001-01-12 13:35:17 +00:00
See
.Sx BUGS
for one short RE using them
1994-05-27 05:00:24 +00:00
that will run almost any system out of memory.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
A backslashed character other than one specifically given a magic meaning
2001-01-12 13:35:17 +00:00
by
.St -p1003.2
(such magic meanings occur only in obsolete
.Bq Dq basic
REs)
1994-05-27 05:00:24 +00:00
is taken as an ordinary character.
2001-01-12 13:35:17 +00:00
.Pp
Any unmatched
.Ql [\&
is a
.Dv REG_EBRACK
error.
.Pp
1994-05-27 05:00:24 +00:00
Equivalence classes cannot begin or end bracket-expression ranges.
The endpoint of one range cannot begin another.
2001-01-12 13:35:17 +00:00
.Pp
.Dv RE_DUP_MAX ,
the limit on repetition counts in bounded repetitions, is 255.
.Pp
A repetition operator
.Ql ( ?\& ,
2001-01-12 13:35:17 +00:00
.Ql *\& ,
.Ql +\& ,
or bounds)
2001-01-12 13:35:17 +00:00
cannot follow another
1994-05-27 05:00:24 +00:00
repetition operator.
A repetition operator cannot begin an expression or subexpression
2001-01-12 13:35:17 +00:00
or follow
.Ql ^\&
or
.Ql |\& .
.Pp
.Ql |\&
cannot appear first or last in a (sub)expression or after another
.Ql |\& ,
i.e., an operand of
2001-01-12 13:35:17 +00:00
.Ql |\&
cannot be an empty subexpression.
An empty parenthesized subexpression,
.Ql "()" ,
is legal and matches an
1994-05-27 05:00:24 +00:00
empty (sub)string.
An empty string is not a legal RE.
2001-01-12 13:35:17 +00:00
.Pp
A
.Ql {\&
followed by a digit is considered the beginning of bounds for a
1994-05-27 05:00:24 +00:00
bounded repetition, which must then follow the syntax for bounds.
2001-01-12 13:35:17 +00:00
A
.Ql {\&
.Em not
followed by a digit is considered an ordinary character.
.Pp
.Ql ^\&
and
.Ql $\&
beginning and ending subexpressions in obsolete
.Pq Dq basic
1994-05-27 05:00:24 +00:00
REs are anchors, not ordinary characters.
2001-01-12 13:35:17 +00:00
.Sh DIAGNOSTICS
1994-05-27 05:00:24 +00:00
Non-zero error codes from
2001-01-12 13:35:17 +00:00
.Fn regcomp
1994-05-27 05:00:24 +00:00
and
2001-01-12 13:35:17 +00:00
.Fn regexec
1994-05-27 05:00:24 +00:00
include the following:
2001-01-12 13:35:17 +00:00
.Pp
.Bl -tag -width REG_ECOLLATE -compact
.It Dv REG_NOMATCH
2002-12-18 12:45:11 +00:00
The
2001-01-12 13:35:17 +00:00
.Fn regexec
2002-12-18 12:45:11 +00:00
function
2001-01-12 13:35:17 +00:00
failed to match
.It Dv REG_BADPAT
invalid regular expression
.It Dv REG_ECOLLATE
invalid collating element
.It Dv REG_ECTYPE
invalid character class
.It Dv REG_EESCAPE
.Ql \e
applied to unescapable character
.It Dv REG_ESUBREG
invalid backreference number
.It Dv REG_EBRACK
brackets
.Ql "[ ]"
not balanced
.It Dv REG_EPAREN
parentheses
.Ql "( )"
not balanced
.It Dv REG_EBRACE
braces
.Ql "{ }"
not balanced
.It Dv REG_BADBR
invalid repetition count(s) in
.Ql "{ }"
.It Dv REG_ERANGE
invalid character range in
.Ql "[ ]"
.It Dv REG_ESPACE
ran out of memory
.It Dv REG_BADRPT
.Ql ?\& ,
.Ql *\& ,
or
.Ql +\&
operand invalid
.It Dv REG_EMPTY
empty (sub)expression
.It Dv REG_ASSERT
2005-02-13 22:25:33 +00:00
cannot happen - you found a bug
2001-01-12 13:35:17 +00:00
.It Dv REG_INVARG
invalid argument, e.g.\& negative-length string
.It Dv REG_ILLSEQ
illegal byte sequence (bad multibyte character)
2001-01-12 13:35:17 +00:00
.El
2005-01-20 09:17:07 +00:00
.Sh SEE ALSO
.Xr grep 1 ,
.Xr re_format 7
.Pp
.St -p1003.2 ,
sections 2.8 (Regular Expression Notation)
and
B.5 (C Binding for Regular Expression Matching).
2001-01-12 13:35:17 +00:00
.Sh HISTORY
Originally written by
.An Henry Spencer .
Altered for inclusion in the
2001-01-12 13:35:17 +00:00
.Bx 4.4
distribution.
2001-01-12 13:35:17 +00:00
.Sh BUGS
1994-05-27 05:00:24 +00:00
This is an alpha release with known defects.
Please report problems.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
The back-reference code is subtle and doubts linger about its correctness
in complex cases.
2001-01-12 13:35:17 +00:00
.Pp
2002-12-18 12:45:11 +00:00
The
.Fn regexec
function
1994-05-27 05:00:24 +00:00
performance is poor.
This will improve with later releases.
2002-12-19 09:40:28 +00:00
The
.Fa nmatch
argument
1994-05-27 05:00:24 +00:00
exceeding 0 is expensive;
2001-01-12 13:35:17 +00:00
.Fa nmatch
1994-05-27 05:00:24 +00:00
exceeding 1 is worse.
2002-12-18 12:45:11 +00:00
The
.Fn regexec
function
2001-01-12 13:35:17 +00:00
is largely insensitive to RE complexity
.Em except
that back
1994-05-27 05:00:24 +00:00
references are massively expensive.
RE length does matter; in particular, there is a strong speed bonus
for keeping RE length under about 30 characters,
with most special characters counting roughly double.
2001-01-12 13:35:17 +00:00
.Pp
2002-12-18 12:45:11 +00:00
The
.Fn regcomp
function
1994-05-27 05:00:24 +00:00
implements bounded repetitions by macro expansion,
which is costly in time and space if counts are large
or bounded repetitions are nested.
An RE like, say,
2001-01-12 13:35:17 +00:00
.Ql "((((a{1,100}){1,100}){1,100}){1,100}){1,100}"
1994-05-27 05:00:24 +00:00
will (eventually) run almost any existing machine out of swap space.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
There are suspected problems with response to obscure error conditions.
Notably,
certain kinds of internal overflow,
produced only by truly enormous REs or by multiply nested bounded repetitions,
are probably not handled well.
2001-01-12 13:35:17 +00:00
.Pp
Due to a mistake in
.St -p1003.2 ,
things like
.Ql "a)b"
are legal REs because
.Ql )\&
is
a special character only in the presence of a previous unmatched
.Ql (\& .
2005-02-13 22:25:33 +00:00
This cannot be fixed until the spec is fixed.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
The standard's definition of back references is vague.
For example, does
2001-01-12 13:35:17 +00:00
.Ql "a\e(\e(b\e)*\e2\e)*d"
match
.Ql "abbbd" ?
1994-05-27 05:00:24 +00:00
Until the standard is clarified,
behavior in such cases should not be relied on.
2001-01-12 13:35:17 +00:00
.Pp
1994-05-27 05:00:24 +00:00
The implementation of word-boundary matching is a bit of a kludge,
and bugs may lurk in combinations of word-boundary matching and anchoring.
.Pp
Word-boundary matching does not work properly in multibyte locales.